• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling get() method or using a variable.

 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Can someone tell me which scenario is more efficient? Calling a get() method 3 or 4 times or calling the get() method only once, put it in a local variable, and then just using the variable. Many thanks!

Barry
 
Yogen Vadnere
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
Second approach sounds good...
1>>>>
int sizeInt = myVector.size() ;
for (;i < sizeInt {<br /> ...<br /> }<br /> 2>>>
for (;i < myVector.size() {
...
}

always prefer 1st approach over 2nd.
correct me if i m wrong.
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally speaking, a method call has overhead (having to do with stack operations and a context shift). A variable refernce is there for significantly faster.
Note, however, that this limitation can often be overcome. Any half decent onfuscator/optimizer should be able inline simple get methods. (That is, in the bytecode, it's treated just as a reference lookup.) You should be able to confirm this by looking at the actual bytecode, and checking for yourself (although be sure to keep obfuscation off, since it will make it difficult to understand). I would also suspect (but have not confirmed) that JITs would be able to figure this out, too.
On the other hand, I would guess (red flag) the same JITs are also smart enough that if you simply do an assignment before the loop, as in Yogen's example, that it's not going to cost you much in the way of performence. I, personally, always do what Yogen did in his example, assign it to a local variable first, as much for readability as anything else.

Disclaimer: I haven't actually checked the bytecode myself, I've just trusted the product descriptions when they say that the code would be inlined. Of course, some would argue that I'm being foolish to do so. Whenever you think something is an optimization, you'd best confirm it for yourself, or, at least, have someone you "trust" confirm it, e.g. co-worker, or well respected author of a book on Java peformence. In my case, I'm trusting a website.
--Mark
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are only calling the get method 3 or 4 times, then it doesn't matter what you do. There are not enough invocations to matter. If however, you call it numerous times, it may matter some, but that depends on the JIT.
The bytecode produced by both scenarios is different. The compiler won't optimize this. However, the JIT most likely will at runtime. In essence, this coding inefficency can be eliminated at runtime with the JIT.
Here is some code and its associated bytecode:

The bytecode produced for foo and bar is:

The for loop in method foo is between 5 and 21. You will notice that the call to size is in the loop at 18. The for loop for method bar is shorter...from 13 to 23.
Again, a good JIT will optimize foo to look like bar. However, don't be surprised if it doesn't. You can always test this by running this code many times and timing it. Run it first without the JIT enabled, and bar should be faster. Then turn the JIT on and if they execute with roughly the same speed, your JIT is doing the job.
My preference would be to code like bar anyway and not worry about whether the JIT was good or not. After all, your code might be run on many different VMs, each with different JITs, or possibly some without a JIT at all.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!