i have observed that the performance will be more when, we use reverse(decreamental) for loop as compared to increamental for loop for looping around 1000000000 times..?
have any one observed this? if so what is the reason behind that increase in performance? please let me know.
It shouldn't make a noticable difference, a != 0 test could in theory be slightly faster than either <10000000 or >0 but you shouldn't be able to measure the difference, and as far as i know these days these tests will be performed equally fasts on all modern cpus. Even if the VM doesn't optimize the entire loop away (it probably won't) it should still be roughly equal. Are you sure you're not measuring VM startup? If i run your test loops above the first is indeed slower, but if i reverse them the other one becomes slower. Either way it's extremely unlikely you'll encounter a performance issue where looping direction will be a significant performance increase ;)
What was the processing time of each? I have heard that decrementing is faster than incrementing though i can't recall if i had heard this about java or not, or even if it is still true with recent jvm's. In most practical examples the time difference would be negligible even if there was a difference. For example in your code presumably you would do something in your loop. Chances are this would take much longer than the effects of incrementing or decrementing. So although an interesting question, in the majority of cases you shouldn't make a distinction between this negligible performance difference.
It is not the increment vs decrement that affects performance but the comparison to something other than zero. If you want even better performance (for you nano-second counters out there) also write as a pre-decrement:
When I was your age I was toggling in the bootstrap code on the front panel
exactly i aggree with above 2 comments (comparing with Zero) but i needed some more detailed information regarding this, i thought zero is neither positive nor negative number hence the JVM takes a less time camparing any value to zero, rather than with other numbers ...
Akshay Mallabadi wrote:exactly i aggree with above 2 comments (comparing with Zero) but i needed some more detailed information regarding this, i thought zero is neither positive nor negative number hence the JVM takes a less time camparing any value to zero, rather than with other numbers ...
can any one provide still light on this .....?
This goes back to operations that are implemented in hardware. Math operations at the machine code level generate not only the result, but in the course, set some flags. Several of these flags are set depending on how the result compares to zero. So, you don't need an extra step to compare a number to zero, the flags are already set appropriately. Comparing to something other that zero, e.g. "i < 1000", usually requires that a subtraction be performed (i - 1000) and that the flag bits are tested after the subtraction.
Joined: Jun 26, 2002
There is a lot of conjecture going on on this thread. The thought is that comparing to 0, but no one has come up with any hard evidence in the literature, or by altering the original tests to keep a 0 test, and test the looping with both increment and decrement.
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: increase in performance for Reverse for loop