• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

For Loop Performance

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for (int i=0;i<arr.length;i++).

In fact, going through the loop counting downwards instead of upwards is faster:

for (int i=arr.length-1;i>=0;i--)

Whether performance is increased by traversing the loop downwards?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

We have a strict policy on display names, which must be a real first and last name with a space between. No last initials, please.

Please go here and fix your display name up, pronto. Thanks, pardner!

Now, as to your question: micro-optimizations like this never make any sense in Java. What's faster on one JVM may be slower on another. More importantly, the HotSpot JVM looks for specific patterns in your bytecode and knows how to optimize them in a platform-specific way; if you use unusual coding idioms, like backwards loops, you may stop HotSpot (or another optimizing JVM or JIT) from doing its job.

So not only should you not do this, you should not even worry about little things like this -- in general, you'll only manage to slow things down by writing tricky code. Write clear, readable code, and trust the JVM to make it fast.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the reason the for loop is faster is that one call is made to arr.length ?
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a another suggestion for the for loop:

What about:

for(int i=arr.length; --i>=0
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neither way is faster in general. Is my post above just too long, that you didn't bother to read it?
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic