Howdy ranchers! I'm still new to this site, so this question might have been asked in the past... I'm interested if these two codes will be different in performance, and if so by how much
It seems to me that the first one would be slower since we create a new reference on every iteration, instead of changing that reference, but the compiler might optimize it (I usually do the second way). Any ideas? [ January 21, 2004: Message edited by: Yuriy Grechukhin ]
The sword of destiny has two blades, one of them is you.
No difference at all. They compile to virtually the same bytecodes. You can check this with "javap -c" if you'd like. The only difference is that since the order of declaration of the two variables "i" and "o" changes, the specific JVM local variables (registers) assigned to then switches.
Ernest is correct. That is because the memory for local variables is typically allocated on the stack frame at the start of a method and freed at the end. There really is no creation or deletion going on in your loop.
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
Originally posted by Ilja Preuss: That is because the memory for local variables is typically allocated on the stack frame at the start of a method and freed at the end.
Aha! That's what was throwing me off. I though for every iteration a new reference would be created on the stack, then at the end, the stack ptr would move up (or down depending on how you look at it). Now I wonder how the performance might suffer if something like this would be implemented through recursion (just for fun).
The sword of destiny has two blades, one of them is you.
Now I wonder how the performance might suffer if something like this would be implemented through recursion (just for fun).
What to implementations do you have in mind?
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
This actually gave me a StackOverflowException when I added 10000 elements But I'm wondering about performance... The last line of output (time) was 370ms (the fastest of 10 runs I did on my system). Here's the code that is without recursion:
Amazingly it seems SLOWER . The last line of output (time) was 400ms (the fastest of 10 runs I did on my system).
The sword of destiny has two blades, one of them is you.
Ok, now the times are the other way around: 361 w/o recursion, 371 with recursion... So I guess the times might the same... They might or they might not, You never can tell with bees. -- Winnie-the-Pooh
The sword of destiny has two blades, one of them is you.
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad: