Over a million developers have joined DZone.

The Hidden Evils Of Java String Methods

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

If you code in Java, you have inevitably used the String.split() and String.replace() (including replaceFirst() and replaceAll()) functions.

And why wouldn't you? They are much more convenient than using the Java Regular Expressions API where you need to create a 'Pattern' object, and possibly a 'Matcher', and then call methods on those.

However, all convenience comes at a price!

The Evil Inside

In this case, the String.split() and String.replace*() methods (with the sole exception of String.replace(char, char) ) internally use the regular expression apis themselves, which can result in performance issues for your application.

Here is the String.split() method:

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

Notice that each call to String.split() creates and compiles a new Pattern object. The same is true for the String.replace() methods. This compiling of a pattern each time can cause performance issues in your program if you call the split() or replace() functions in a tight loop.

Benchmark

I tried a very simple test case to see how much the performance is affected. 

The first case used String.split() a million times:

public static void main(String[] args) 
{
	long begin = System.nanoTime();
	
	ArrayList<String[]> list = new ArrayList<String[]>(1000000);
	for (int i = 0; i < 1000000; i++)
	{
	  String[] split = "Hello World".split(" ");
	  list.add(split);
	}

	long end = System.nanoTime();
	System.out.println(list.size());

	System.out.println(TimeUnit.MILLISECONDS.convert(end-begin,TimeUnit.NANOSECONDS));
}

In the second case, I just changed the loop to use a precompiled Pattern object:

//create the Pattern object outside the loop	
Pattern pattern = Pattern.compile(" ");

for (int i = 0; i < 1000000; i++)
{
	String[] split = pattern.split("Hello World", 0);
	list.add(split);
}

Benchmark Results

Here are the average results of 6 test runs:

Time taken with String.split() : 1600ms
Time taken with precompiled Pattern object: 1195 ms

Split-benchmark

Conclusion

Note that I used an extremely simple regular expression here which consists of just a single 'space' character and it resulted in > 25% decrease in performance.

A longer more complex expression would take longer to compile and thus make the loop containing the split() method even slower compared to its counterpart.

Lesson learned: It is good to know the internals of the APIs you use. Sometimes the convenience comes at the price of a hidden evil which may come to bite you when you are not looking.

 

From http://eblog.chrononsystems.com/hidden-evils-of-javas-stringsplit-and-stringr

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}