DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > The Hidden Evils Of Java String Methods

The Hidden Evils Of Java String Methods

Prashant Deva user avatar by
Prashant Deva
·
Feb. 23, 12 · Java Zone · Interview
Like (0)
Save
Tweet
11.57K Views

Join the DZone community and get the full member experience.

Join For Free

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

Java (programming language) Strings

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Open Source Monitoring and Metrics Landscape
  • No-Code/Low-Code Use Cases in the Enterprise
  • Top 7 Features in Jakarta EE 10 Release
  • Top 10 Automated Software Testing Tools

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo