This week's book giveaway is in the Object Relational Mapping forum.
We're giving away four copies of Pro JPA 2: Mastering the Java Persistence API and have Mike Keith and Merrick Schincariol on-line!
See this thread for details.
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Performance
 
RSS feed
 
New topic
Author

split vs substring

fahad siddiqui
Ranch Hand

Joined: Jun 14, 2006
Messages: 85

Which would give me a better performance advantage?
for substring, i would be first checking the indexOf and then subtringing in 2 parts.

The string would break to a max of 2 parts.
Jeanne Boyarsky
internet detective
Sheriff

Joined: May 26, 2003
Messages: 17131

Neither is likely to be time consuming enough to be a bottleneck. Go with the one that is clearer and easier to maintain - in this case split().

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]

"The set strikes me as something like the set of potatoes, radishes, farming, and lunch. " - a colleague's way of comparing both overlapping and disparate groups. made me laugh and thought of the ranch
rajesh bala
Ranch Hand

Joined: Jan 14, 2003
Messages: 66

split() internally creates Pattern.compile which is an expensive operation. so I would say, use split() in an efficient way. Or use Pattern.compile(regex).split(this, limit); directly.

~Rajesh.B
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Messages: 14091

I'm with Jeanne here - simply use split() until it is proven to be a bottleneck for you application.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
rajesh bala
Ranch Hand

Joined: Jan 14, 2003
Messages: 66

FYI...

This is the snippet from String.java.

public String[] split(String regex, int limit) {
return Pattern.compile(regex).split(this, limit);
}

If you already know what you are trying to split, its always better to compile the regex and compile the pattern only once.

~Rajesh.B
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Performance
 
RSS feed
 
New topic
MyEclipse Enterprise Workbench

.