Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to iterate an Array in Java: loop variable vs enhanced for statement [duplicate]

In Java, is it faster to iterate through an array the old-fashioned way,

for (int i = 0; i < a.length; i++)     f(a[i]); 

Or using the more concise form,

for (Foo foo : a)     f(foo); 

For an ArrayList, is the answer the same?

Of course for the vast bulk of application code, the answer is it makes no discernible difference so the more concise form should be used for readability. However the context I'm looking at is heavy duty technical computation, with operations that must be performed billions of times, so even a tiny speed difference could end up being significant.

like image 399
rwallace Avatar asked Jun 17 '09 11:06

rwallace


People also ask

Is enhanced for loop faster Java?

For a low number of iterations (100-1000), the enhanced for loop seems to be much faster with and without JIT. On the contrary with a high number of iterations (100000000), the traditional loop is much faster.

Which is faster iterator or for loop in Java?

Iterator and for-each loop are faster than simple for loop for collections with no random access, while in collections which allows random access there is no performance change with for-each loop/for loop/iterator.

Which is better in terms of performance for iterating an array?

Correct Option: A. reverse traversal of array take half number cycles as compared to forward traversal.

Are enhanced for loops more efficient?

It's the bast choice, because it works fine with all types of lists. It also expresses the intent more clearly, and is safer because you don't risk to increment the index inside the loop, or use the wrong index in case of nested loops.


1 Answers

If you're looping through an array, it shouldn't matter - the enhanced for loop uses array accesses anyway.

For example, consider this code:

public static void main(String[] args) {     for (String x : args)     {         System.out.println(x);     } } 

When decompiled with javap -c Test we get (for the main method):

public static void main(java.lang.String[]);   Code:    0:   aload_0    1:   astore_1    2:   aload_1    3:   arraylength    4:   istore_2    5:   iconst_0    6:   istore_3    7:   iload_3    8:   iload_2    9:   if_icmpge   31    12:  aload_1    13:  iload_3    14:  aaload    15:  astore  4    17:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;    20:  aload   4    22:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V    25:  iinc    3, 1    28:  goto    7    31:  return 

Now change it to use an explicit array access:

public static void main(String[] args) {     for (int i = 0; i < args.length; i++)     {         System.out.println(args[i]);     } } 

This decompiles to:

public static void main(java.lang.String[]);   Code:    0:   iconst_0    1:   istore_1    2:   iload_1    3:   aload_0    4:   arraylength    5:   if_icmpge   23    8:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;    11:  aload_0    12:  iload_1    13:  aaload    14:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V    17:  iinc    1, 1    20:  goto    2    23:  return 

There's a bit more setup code in the enhanced for loop, but they're basically doing the same thing. No iterators are involved. Furthermore, I'd expect them to get JITted to even more similar code.

Suggestion: if you really think it might make a significant difference (which it would only ever do if the body of the loop is absolutely miniscule) then you should benchmark it with your real application. That's the only situation which matters.

like image 122
Jon Skeet Avatar answered Oct 14 '22 01:10

Jon Skeet