Recently I was wondering what the performance difference really is between using a String literal and using a final static String in a Java application. I have always heard that using final static String is significantly faster. So I wrote a small program that would measure the time to print a String literal and the time to print a final static String. Here are some of the conditions I used:
- Both strings are the same length (arbitrary 39 characters) but different strings for each.
- I used System.out.println() for each case
- I ran an arbitrary 100 iterations of doing the same println for each in hopes of eliminating any initialization latency.
- Java 1.6 is used
- I used "System.nanoTime()" to get the timings in each case.
To my surprise the String literal printed faster than the final static String in the tests 60 to 70% of the time. In some cases the String literal printed dramatically faster. Now we are talking about timings in the range of between 15000 nanoseconds to 35000 nanoseconds in each test. However if the application must execute some components at less than one millisecond for instance, this could be influential.
I'd like to know if others have ever run this test and what their findings are.
I am also looking the tests over to see if there are any "gotchas" I might of missed.
Roger Ball wrote:Recently I was wondering what the performance difference really is between using a String literal and using a final static String in a Java application. I have always heard that using final static String is significantly faster.
That makes no sense. The two are not mutually exclusive. And it's not clear what you mean by "using".
So I wrote a small program that would measure the time to print a String literal and the time to print a final static String.
The I/O will massively dwarf any CPU time used to access the Strings. Your test may also be flawed in other ways, but without seeing it, it's impossible to say.
And finally, whatever you mean by "literal vs. final static," the performance will probably never matter, and you will never choose based on that. You pick the one that better fits your requirements and design.
Oh, and welcome to the Ranch!
Joined: Jul 08, 2003
Thanks for the ideas. I changed my test to only do an assignment. No I/O. Here are the tests
String Litteral Test
final static Sting test
I ran both tests in a loop 100 times to get an average, still the assignment of a String literal was faster than the assignment of a final static String in between 60 to 70 % of the cases. Now, granted the actual assignment times are very low, in most cases between 100 and 300 nanoseconds. So for most applications this would not be a measurable factor but for an application that must execute a less than 1 millisecond it could be a factor.
1) The intervals you're timing are way too small. You need at least thousands and possibly 10k, 100k, or 1M assignments to get a reasonable test.
2) System.nanoTime() is not guaranteed to give nano accuracy. It can be more accurate than currentTimeMillis() on some systems, but it might not, and even if it is, you don't know how accurate.
3) The length of the String has precisely zero effect. We are not copying String objects, just references.
4) Finally, most importantly, whether assigning from a private static final String variable or from a String literal, the bytecode is identical because they're both compile-time constants. Therefore, there cannot be a consistent, predictable performance difference between the two. See below.