File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes increase in performance for Reverse for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "increase in performance for Reverse for loop" Watch "increase in performance for Reverse for loop" New topic
Author

increase in performance for Reverse for loop

Akshay Mallabadi
Greenhorn

Joined: Aug 17, 2009
Posts: 2
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

Joined: Nov 10, 2007
Posts: 144
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

Joined: Jun 26, 2002
Posts: 860
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.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: Nov 19, 2001
Posts: 174

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

Joined: May 31, 2007
Posts: 3219
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.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Akshay Mallabadi
Greenhorn

Joined: Aug 17, 2009
Posts: 2
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

Joined: Nov 19, 2001
Posts: 174

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

Joined: Jun 26, 2002
Posts: 860
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: increase in performance for Reverse for loop