If the question is what I think it is -- "Is member variable access or local variable access faster?" -- then the answer is actually m2(), because local variable access is considerably faster. But as David correctly points out, this difference will be lost in the noise of allocating thousands of Strings in both of these methods. You won't be able to measure a difference.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I think that it depends on the mechanism running behind, the memory allocation, the status of memory fragmentation...etc. I think that many conditions will be included when we want to know which one is faster.
str is never initalized, so I would think it would give a null pointer exception
No, it will run just fine...
I am sure that the difference is purely academic, and it would be almost impossible to tell the difference... but in theory, m2() should be faster.
“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Joined: May 24, 2004
Just in case anyone is curious (or bored like I was), I ran a timing test calling each of these methods 1000 times. Here are the results (no big shock):
Average time for m1(): 33 ms Highest time for m1(): 62 ms Lowest time for m1(): 31 ms
Average time for m2(): 33 ms Highest time for m2(): 47 ms Lowest time for m2(): 31 ms
Joined: Jul 11, 2001
Originally posted by Paul Bourdeaux: Just in case anyone is curious (or bored like I was), I ran a timing test calling each of these methods 1000 times. Here are the results (no big shock):
Two important suggestions for java microbenchmarks in general:
- have you tried to increase the number of iterations? - have you tried to switch the order of execution?
Both things are important to notice the effect of the ongoing optimization of the hotspot engine...
Joined: Jun 26, 2002
I would guess many more executions than 1000 would be needed to show a performance difference. On my rather old pc I can execute 60,000,000 method executions in 1 second (a noop method call), and referencing a variable should be much faster than that! That is why we say the question is purely academic.
Being as referencing instance varable/local variables would both be so fast it would be hard to accurately measure a performance difference with the much more expensive string concatenation in the loop. For example the garbage collector or some other process on the computer may fire during one or the other of the routines and make the comparisons meaningless. I would avoid all the object allocation that is done in this example.
Also, even if you measure a performance difference I suspect it may be different in another JVM, or on another platform. So your new found knowledge may be very transient.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Which of the two methods will execute faster