Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using StringUtils methods runs faster than Java methods?

I want to know if the methods from StringUtils from Apache Commons Lang library runs faster than Java String methods.

I know this is micro-optimization, but it's necessary because methods will be executed millions times.

Methods that using regex in Java like split() or String comparison with equals().

like image 302
Renato Dinhani Avatar asked Oct 26 '11 19:10

Renato Dinhani


3 Answers

Both the methods in the Java String class and the methods in StringUtils are highly optimised for the specific tasks they were designed to solve. Different methods are designed to solve slightly different tasks. Try to pick the method that most closely matches the task you are trying to solve, and it's likely you will get very good performance.

If there are two possible approaches that look equally good, and it's vital to get every last bit of performance, then measure it! Time the performance of both approaches on your actual data.

But also remember that it's often cheaper to buy a faster computer instead of spending hours of development time micro-optimizing your code to get it to run fast on a slow machine.

like image 132
Mark Byers Avatar answered Nov 17 '22 02:11

Mark Byers


Well it probably depends on each individual method. Most of the methods represent very basic code that people often have to write by hand such as

return s1 == s2 || (s1 == null && s2 == null) || (s1 != null && s1.equals( s2 ) );

This code would very likely be optimised to the same thing by the hotspot compiler removing even the method call. I would guess that the only savings in time you'll find are in writing.

As suggested elsewhere, I would just recommend benchmarking the methods you are interesting in yourself use System.currentTimeMillis() around a loop that calls the method on some random strings; random because you don't want it optimised away.

like image 35
Sled Avatar answered Nov 17 '22 02:11

Sled


I do not think it is anyhow optimized, Apache Commons contains 'helper utilities', meaning it it is more efficient for development not for run-time performance efficiency.

The performance may also change with JVM used.

I can understand, that you worry about performance with large number of executions or big data. But that does not mean that the micro-optimization fallacy does not apply with regards to this. It is certainly more efficient to spend more time thinking about and trying out other ways to improve performance of such rudimentary tasks (running not in one but in as many threads as there are CPUs on the machine, etc.). It is wiser to leave such low level stuff for the JVM. The just-in-time-compiler is usually much smarter than any developer in my experience.

If you really want to go that way, you should test it. it should not be that difficult and in case the performance depends on data, you might have the right one to test it with.

like image 1
MarianP Avatar answered Nov 17 '22 00:11

MarianP