This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes For Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For Loop" Watch "For Loop" New topic

For Loop

satya sahu
Ranch Hand

Joined: Mar 26, 2003
Posts: 97
I have two for loops,
for(int i = 0; i < 10000, i++)
for(int i = 10000, i>=0; i--)
Which one is faster and why?
This was asked to me in some interview, and i could not answer.
I searched through the Net, but not getting any answer.
Can anybody help me to understand the correct answer of this.
Thanks in advance.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37945
Originally posted by satya sahu:
I have two for loops,
for(int i = 0; i < 10000, i++)
for(int i = 10000, i>=0; i--)
Which one has more iterations in and why?
Note the slight change I have made to that quote.

The correct answer to your actual question is "don't know, don't care." You program for correctness and if you want to iterate one direction you use one loop and for the other direction you use the other, conveniently ignoring the fact that one of those loops might (in a very sensitive situation) cause an Exception to be thrown. You make performance your lowest priority.

There was a thread about 2 years ago about "is repeated i++ faster than repeated i--" which you might find in a search; I tried out a simple application to test that suggestion and I can't remember which was faster. But I can remember it was only slightly faster and occasionally the other operation was faster. So not only is performance a low priority, it is also unpredictable and variable.

Let's see what everybody else says, now!
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
I wrote a simple test:

and here are the results on my machine:

The first time a for loop is encountered it takes a large time (making the ++ loop execute earlier than the -- loop produces a similar result). But for the rest of the runs, there seems to be no difference between both the approaches.
Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2716

I agree with Campbell, and as you can see in the above post, performance isn't the concern.
I remember in C programming, at times we missed the underflow condition the program would loop indefinitely.

|Asking Good Questions|
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11153

just to be a stickler, the second won't compile as-is.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor

Joined: Oct 27, 2005
Posts: 19651

Neither will the first. Both have a , where a ; should be.

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37945
And please confirm that the difference between 59225 and 59226 actually means anything!

I suspect, but might be mistaken, that there is some just-in-time compilation going on which enables the loops both to run faster on successive executions.
jQuery in Action, 2nd edition
subject: For Loop
Similar Threads
multiples project no output
Program like ATM where person enters amount in dollars and cents but program uses int for monies
Creating a unique random number...
Random 5-Digit number
Math.random() !!!!!!