This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Performance and the fly likes why backward access is faster when compared to forward looping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "why backward access is faster when compared to forward looping " Watch "why backward access is faster when compared to forward looping " New topic
Author

why backward access is faster when compared to forward looping

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi ,

I have heard that accessing an Array or a List from backwards is faster when forward looping .

For example the Backward access of an List .



Please tell me why backward access is faster when compared to forward looping


Save India From Corruption - Anna Hazare.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It isn't. Just because somebody somewhere said that doesn't make it true.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

A few minutes of testing could have shown you this.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1756
    
    7

I very seriously doubt it makes any difference.
Even if it somehow did, the difference would be so minute that the effect on overall system performance would be insignificant at best.
This is a "performance optimization" that is not worth consideration, certainly not if code clarity gets sacrificed in the process, which it usually does.

Edit: Yay, cpt. slow does it again




Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

There are things that could be true and things that are true.

I've heard two urban legends in the space.
  • 1) It is better to store list.size() in a variable so it doesn't get called multiple times. Not true because size() is not an expensive operation. If size() was implemented by looping through the list, things would be different.
  • 2) It is better to loop backwards. The theory is that it is faster to compare with zero. Don't know if it is true or not but it certainly doesn't make enough of a difference to be worth doing. And the posts above tell me it isn't true anyway.


  • [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
    Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
    William Brogden
    Author and all-around good cowpoke
    Rancher

    Joined: Mar 22, 2000
    Posts: 12761
        
        5
    Oh that old programmer legend - I saw that "hint" when Java 1.02 was new. JIT optimization was just an idea then.

    Those "hints" just get in the way of writing clear and maintainable code.

    Bill

    steve souza
    Ranch Hand

    Joined: Jun 26, 2002
    Posts: 860
    All the excellent responses above warm my performance tuning heart. I suspect if we look at this forum in 10 years the same question will be asked and no one will remember where this urban myth came from or even if it was ever true.


    http://www.jamonapi.com/ - a fast, free open source performance tuning api.
    JavaRanch Performance FAQ
    D. Ogranos
    Ranch Hand

    Joined: Feb 02, 2009
    Posts: 214
    Jeanne Boyarsky wrote:1) It is better to store list.size() in a variable so it doesn't get called multiple times. Not true because size() is not an expensive operation. If size() was implemented by looping through the list, things would be different.


    Why would you call a method x-hundred or thousands of times when you can simply store its (constant) value in a variable? This is in fact a tip found in the book "Effective Java". Although maybe a good compiler optimizes it that way on its own already. And by doing this, there really shouldn't be any difference between forward- and backward-looping.
    Jeanne Boyarsky
    internet detective
    Marshal

    Joined: May 26, 2003
    Posts: 30130
        
    150

    D. Ogranos wrote:Why would you call a method x-hundred or thousands of times when you can simply store its (constant) value in a variable?

    I'm not saying you would. I'm saying there isn't a performance difference anymore. (if there ever was.) It is a variable in list's implementation too so the only thing being eliminated is the method call.

    I would still eliminate the method call for clarity - to show that I don't expect the value to change once I enter the loop.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: why backward access is faster when compared to forward looping
     
    Similar Threads
    New type of for loop?
    listview with checkbox problem
    ArrayList with Iterator new*
    compiler error-incompatible types
    sum of non numeric elements in an ArrayList