• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

increase in performance for Reverse for loop

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ;)
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic