Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whether Using final With Local Variable Will Increase Performance?

 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am a bit curious to know about using final keyword with local variables.
Will it increase the performance or it's just a good programming style to use final with local variable?

Thanks,
Tanzy.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it. Use the System.nanoTime method before and after a method call, and see whether it makes any difference.
Use the javap tool with the -c option to print out the bytecode, and see whether there is any difference.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the value for the final variable is a primitive or String literal, or a String that is created by appending primitives and String literals (a.k.a. compile time constants), those will be inlined. For example:
All three will have all their references replaced by their value. For instance, System.out.println(s2) will actually be compiled as System.out.println("13Hello481.0").

If the final variable is constructed using anything else, including the results of any method, then the above does not hold.
 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob for the valuable response.

Well, if we are using final with local variable and the the value depends on runtime,
then how about performance?

For instance--



which one is better to use?


Thanks,
Tanzy.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In those cases just follow Campbell's advice.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The general rule that most programmers SHOULD live by is this:

Write code that makes sense, that does what you need, and is simple.

ONLY then, if you have a performance issue, should you consider optimizing it. and then, use a tool to find out where the slowdowns are. 99.9% of the time, the slowdown is somewhere you are not looking, and you can spend hours changing one thing to gain practically nothing, while making a modest change somewhere else could give HUGE boosts.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read this article written by Brian Goetz on usage of final keyword in code.
 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you rosenberger for giving such a nice advice.

Thank you Ritchie for giving nice trick to chek performance issues.

Thank you Tripathi for giving such a nice link.

Thank you all for valuable answers and suggestions.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic