• 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

For Loop

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have two for loops,
for(int i = 0; i < 10000, i++)
and
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.
 
Marshal
Posts: 79405
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by satya sahu:
Hi,
I have two for loops,
for(int i = 0; i < 10000, i++)
and
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!
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just to be a stickler, the second won't compile as-is.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neither will the first. Both have a , where a ; should be.
 
Campbell Ritchie
Marshal
Posts: 79405
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
catch it before it slithers away! Oh wait, it's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic