• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String literal vs final static String - Performance surprise

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks everyone
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Roger Ball
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jeff
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Several points:

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.

 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The I/O will massively dwarf any CPU time used to access the Strings



True. SOP is horribly inefficient at logging anything.

Roger, what exactly are you trying to measure ? The access time for literal Vs final static Strings ? Going by the bytecode posted by Jeff, there does not seem to be any difference.
reply
    Bookmark Topic Watch Topic
  • New Topic