Performance: Iterating through a List in Java

前端 未结 9 2108
半阙折子戏
半阙折子戏 2020-12-29 20:33

Is it slower to iterate through a list in Java like this:

for (int i=0;i

as opposed to:

相关标签:
9条回答
  • 2020-12-29 20:54

    There shouldn't be any noticeable differences in performance for normal lists.

    For linked lists, the iterator will be substantially faster, especially for large lists.

    0 讨论(0)
  • 2020-12-29 20:57

    THERE CAN BE A DIFFERENCE.

    If a List implementation also implements java.util.RandomAccess (like ArrayList does), then it is just about faster to use its get() method over an interator.

    If it does not implement java.util.RandomAccess (for example, LinkedList does not), then it is substantially faster to use an iterator.

    However, this only matter if you are using lists containing thousands of (possibly scattered) objects or that are constantly traversed (as if performing copious amounts of math on List objects representing numerical vectors.)

    0 讨论(0)
  • 2020-12-29 21:03

    No. It is faster (or should be faster) when the list also implements: RandomAccess (as ArrayList does and LinkedList doesn't).

    However, you should always use the latter :

     for( Object o: list ) {
     }
    

    and only switch to the former if your have substantial evidence that you're having a performance issue using it (for instance, you profile your application and as result you see as a point for improvement this section of code).

    By not doing so, you risk not being able to switch your implementation in a later refactoring if your application requires it (because you would be tied to the for( int i = 0 ; i < list.size(); i++ ) idiom).

    0 讨论(0)
  • 2020-12-29 21:04

    According to benchmark tests in While loop, For loop and Iterator Performance Test – Java and the JavaRanch question "is using ArrayList.iterator() is faster than looping ArrayList in for loop?" the iterator is actually a bit slower.

    I'd still favor readability unless I'd benchmarked my entire application and found that loop to be my bottleneck, though.

    0 讨论(0)
  • 2020-12-29 21:06

    It is recognized that the distinction between random and sequential * access is often fuzzy. For example, some List implementations * provide asymptotically linear access times if they get huge, but constant * access times in practice. Such a List implementation * should generally implement this interface. As a rule of thumb, a * List implementation should implement this interface if, * for typical instances of the class, this loop: *

     *     for (int i=0, n=list.size(); i < n; i++)
     *         list.get(i);
     * 
    * runs faster than this loop: *
     *     for (Iterator i=list.iterator(); i.hasNext(); )
     *         i.next();
     * 
    *

    0 讨论(0)
  • 2020-12-29 21:07

    Created a microbenchmark for the question and was surprised to see for-each runing 2x-3x faster than an indexed loop. The only explanation I have is that for-each version might not require range checks which are made by ArrayList.get(int index).

    For very small lists (10 elements) the result was about the same. For 100 elements for-each is 1.5x faster, for 10000-100000 elements it is faster 2x-3x times.

    The tests are run on a random dataset and checksums are being verified at the end, so JIT chearing is very unlikely to take place in these.

    0 讨论(0)
提交回复
热议问题