Monday, 27 February 2012

Guava Splitter vs StringUtils

So I recently wrote a post about good old reliable Apache Commons StringUtils, which provoked a couple of comments, one of which was that Google Guava provides better mechanisms for joining and splitting Strings. I have to admit, this is a corner of Guava I've yet to explore. So thought I ought to take a closer look, and compare with StringUtils, and I have to admit I was surprised at what I found.

Splitting strings eh? There can't be many different ways of doing this surely?

Well Guava and StringUtils do take a sylisticly different approach. Lets start with the basic usage.

// Apache StringUtils...
String[] tokens1 = StringUtils.split("one,two,three",',');

// Guava splitter...
Iterable<String> tokens2 = Splitter.on(',').split("one,two,three");

So, my first observation is that Splitter is more object orientated. You have to create a splitter object, which you then use to do the splitting. Whereas the StringUtils splitter methods uses a more functional style, with static methods.

Here I much prefer Splitter. Need a reusable splitter that splits comma separated lists? A splitter that also trims leading and trailing white space, and ignores empty elements? Not a problem:

Splitter niceCommaSplitter = Splitter.on(',')

niceCommaSplitter.split("one,, two,  three"); //"one","two","three"
niceCommaSplitter.split("  four  ,  five  "); //"four","five"
That looks really useful, any other differences?

The other thing to notice is that Splitter returns an Iterable<String>, whereas StringUtils.split returns a String array.

Don't really see that making much of a difference, most of the time I just want to loop through the tokens in order anyway!

I also didn't think it was a big deal, until I examined the performance of the two approaches. To do this I tried running the following code:

final String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";

long start = System.currentTimeMillis();  
for(int i=0; i<1000000; i++) {
    StringUtils.split(numberList , ',');   
System.out.println(System.currentTimeMillis() - start);
start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
    Splitter.on(',').split(numberList );
System.out.println(System.currentTimeMillis() - start);

On my machine this output the following times:


Guava's Splitter is almost 10 times faster!

Now this is a much bigger difference than I was expecting, Splitter is over 10 times faster than StringUtils. How can this be? Well, I suspect it's something to do with the return type. Splitter returns an Iterable<String>, whereas StringUtils.split gives you an array of Strings! So Splitter doesn't actually need to create new String objects.

It's also worth noting you can cache your Splitter object, which results in an even faster runtime.

Blimey, end of argument? Guava's Splitter wins every time?

Hold on a second. This isn't quite the full story. Notice we're not actually doing anything with the result of the Strings? Like I mentioned, it looks like the Splitter isn't actually creating any new Strings. I suspect it's actually deferring this to the Iterator object it returns.

So can we test this?

Sure thing. Here's some code to repeatedly check the lengths of the generated substrings:

final String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
long start = System.currentTimeMillis();  
for(int i=0; i<1000000; i++) {
  final String[] numbers = StringUtils.split(numberList, ',');
    for(String number : numbers) {
System.out.println(System.currentTimeMillis() - start);
Splitter splitter = Splitter.on(',');
start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
  Iterable<String> numbers = splitter.split(numberList);
    for(String number : numbers) {
System.out.println(System.currentTimeMillis() - start);

On my machine this outputs:


Guava's Splitter is almost 4 times slower!

Indeed, I was expecting them to be about the same, or maybe Guava slightly faster, so this is another surprising result. Looks like by returning an Iterable, Splitter is trading immediate gains, for longer term pain. There's also a moral here about making sure performance tests are actually testing something useful.

In conclusion I think I'll still use Splitter most of the time. On small lists the difference in performance is going to be negligible, and Splitter just feels much nicer to use. Still I was surprised by the result, and if you're splitting lots of Strings and performance is an issue, it might be worth considering switching back to Commons StringUtils.


Loboplaya said...

Very interesting article. I often hesitate between Google Guava or Apache.

Guava feels more modern though, and Google will probably iron out (most of) the performace issue you found.

No risk that Guava is shut down anyway :-) .

Sean said...

The root cause is array much faster than iterable

Anonymous said...

Thank you for the comparison. I wonder if by some trick the performance of Iterable iteration could be matched with iterating over an array in future versions of Java, or if that's impossible?

Stanislav Chetvertkov said...
This comment has been removed by the author.
David E. Jacobs said...

As for me, i don't that .They have to change their opinion about whom to f*ck, cuz that's rea'lly delirious. Even an essay writer proved my words

Алексей Бондарчук said...

Nice effort, very informative, this will help me to complete my task. Thanks for share it keep it up.

Fred Duffler said...

I write programm. And thanks to your blog I don't have any problem with this!

Алексей Бондарчук said...

great)(www masterpapers com) liked everything very much) keep it up and dont stop)

Nikolo Lloyd said...

Some website page - precisely what I would like. We seemed following a anything for a little bit to analyze on by myself periodical, and yes it goes apart a both equally I had put together no think this issue, nor I won't will perform essentially an extended evaluation. Your internet site features amenable youthful methods with myself, any time I can assert now, Which i value.
write an essay

Reem Halevi said...

Sorry to spoil the fun but...

It’s not actually a matter of bad performance ,only bad testing …
You were probably testing this benchmark with a default JVM parameters which is a small JVM size (default 64MB) meaning you had a Garbage Collection during your test since you are creating 2M arrays of 10 strings each – a fact that prolonged your second test.

I gave it a test with a BIG enough JVM (e.g: -Xms2024M -Xmx2024M ) and both splitter and StringUtils are quite the same
See my execution results (in ms) with -Xms2024M -Xmx2024M:


And with a default JVM size (default 64MB) i got number as you posted :


your test was incorrect you had GC while testing which prolonged splitter execution time - please enlarge your JVM accordingly to the test.

get rid of cellulite said...

The post is absolutely fantastic! Lots of great information and inspiration, both of which we all need! Also like to Logo design admire the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often.
marmeren keukenwerkblad

DY DEO said...

I absolutely respect and appreciate your point on each and every object.

mehroz aftab said...

Thanks for excellent blog.I'm happy to find useful blog share in my friends. THANK YOU
stenen tafelblad

sheraz ajaz said...

I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great chats

Alexander Alex said...

This is a great article but very debatable. Im going to say that Steve Jobs absolutely loves the App store simply because it has been such a huge part of the iPhones success top gadgets reviews online. It has made the iPhone so much more appealing simply because of all the awesome apps that developers have created for Steve Jobs phone, the iPhone.

aliya seen said...

The website proofreading is excellent service to have. We just need to realize the efforts.

Akula Rahul said...

MPCDF Technician Assistant Grade 3 Recruitment 2016

This is awesome blog with smart content, Nice to see your post. Thanks for wonderful presentation.....

Paul Smith said...

I'm so grateful for such a detailed presentation. To tell the truth, this is the best publishing I have ever read. Don't you want to become our paper writer? Just keep in mind that unlike other organizations, we offer flexible working hours.

Manoj Kumar said...

nic post...

Kristofer Kihn said...

I fancied to thank you for this uncommon read!!
MATLAB Homework Help

Ken Smith said...

Thank you for a great post! 
Project Management Capstone Project Writing Service

David Richard said...

Thanx for sharing such useful post keep it up :)
writing essay service

Lewis Owen said...

Electrical Assignment Help
Excellent post. This is a very good blog that I will definitively come back

Kits Online Training said...

nice blog, thanks for sharing
Sap SD Online Training by Real Time Faculties

Best SQL Server Developer Online Training Institute By RealTime Faculties

Oracle EnterPrise Manager Online Training Institute From India|US|UK

Best Oracle RAC Online Training From US|UK|CANADA|

Alamin Miah said...

Skin needling scars introduced by Dr Roller skin roller remedies is a effective as well as simple means to eliminate marks from your body This remedy of scar extraction is likewise secure as it does not have any damaging acidic active ingredients or make use of any laser treatment.

Alamin Miah said...

When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four e-mails with the same comment. Is there any way you can remove people from that service? Thank you!

Albert Smith said...

I am very passionate about programming and I have been learning different programming languages online. I have outsourced a lot of programming information, knowledge, and skills from Tom’s programming blog and I am very grateful. I found thus link to be very useful find the time and check it out Professional Thesis Service Providers.

Alamin Miah said...

In other situations, you might be concerned if that position requires the employee to operate with youngsters, sensitive donor information, etc. My Blog

Mona afrin said...

Hello There. I discovered your blog the use of msn. This is an extremely smartly written article. I’ll make sure to bookmark it and come back to learn extra of your useful info. Thank you for the post. I will definitely comeback. My Blog

Mona afrin said...

Spread betting and CFD trading may bring about failures that exceed your original deposit and take a higher degree of danger to your capital. My Blog

Neil Jakson said...

I truly appreciate this post. I’ve been looking everywhere for this! Thank goodness I found it on Bing. You’ve made my day! Thank you again! -
dissertation Writing Service

Gajendra jain said...

More readable & more helpful for me of your blog. I'm glad to read of your blog.
Please add my site- www(.)proofreadmyfile(.)com(/)

Mazhar Islam said...

Thank you for the comparison. I wonder if by some trick the performance of Iterable iteration could be matched with iterating over an array in future versions of Java, or if that's impossible.
how to do PHP coding in my website for contact us page.

Shahid Raza said...

Booking of Cheap Umrah Package December 2017 with discounted price. Best Umrah Package from UK.
For best: Cheap Umrah Package December 2017
Contact Us:

imran shabbir said...

Book the cheapest Umrah tour package UK by consulting with our tour operator agents. We consider detailed aspect of all deals before making the offer to UK citizens.
For best Umrah Tour packages Uk
Contact Us:

imran said...

Cheap Umrah Deals UK and Hajj packages 2017 by Cheapest hajj and Travel offered with star Hotels and Luxury flights from UK, London.
For best: Cheap Umrah Deals UK

Taj Nin Niha said...

Hi Administrator,
I am really pleased to come here. Amazing things you have been shared. Thank you for sharing this type of informative blog. please keep posting like that.

well, this is my site video editing services

you may visit and can share your opinion with us.

kezia McDonald said...

Book the cheapest Umrah tour package UK by consulting with our tour operator agents. We consider detailed aspect of all deals before making the offer to UK citizens.

kezia McDonald said...

Book the cheapest Umrah tour package UK by consulting with our tour operator agents. We consider detailed aspect of all deals before making the offer to UK citizens.