Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

increase in performance for Reverse for loop

 
Akshay Mallabadi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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.


code:

for(int i=0;i<1000000000;i++){

}
System.out.println("hi");


and

for(int i=1000000000;i>0;i--){

}
System.out.println("hi");
 
R van Vliet
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akshay Mallabadi wrote:Hello,

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.


code:

for(int i=0;i<1000000000;i++){

}
System.out.println("hi");


and

for(int i=1000000000;i>0;i--){

}
System.out.println("hi");


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 ;)
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Waaaaay back in the beginning of Java there was this folklore that decrementing was faster. The clearest possible example of "premature optimization is the root of all evil."

Bill
 
Carey Brown
Ranch Hand
Pie
Posts: 1335
15
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
arulk pillai
Author
Ranch Hand
Posts: 3385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compromising on readability for a small performance gain is not worth it. I think the performance gain is to do with comparing it to zero.
 
Akshay Mallabadi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .....?
 
Carey Brown
Ranch Hand
Pie
Posts: 1335
15
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic