File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why backward access is faster when compared to forward looping

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't. Just because somebody somewhere said that doesn't make it true.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64194
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes of testing could have shown you this.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  •  
    William Brogden
    Author and all-around good cowpoke
    Rancher
    Posts: 13048
    6
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 862
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    D. Ogranos
    Ranch Hand
    Posts: 214
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    author & internet detective
    Marshal
    Posts: 33697
    316
    Eclipse IDE Java VI Editor
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic