Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong><em>It is better to use for-each loop</em></strong> [more readable]</p> <pre><code>for (Flower flower :flowers){ //... } </code></pre> <hr> <p>I have dumped instructions using <code>javap</code> for the following code:</p> <pre><code>public void forLoop1() { List&lt;String&gt; lst = new ArrayList&lt;String&gt;(); for (int i = 0; i &lt; lst.size(); i++) { System.out.println("hi"); } } public void forLoop2() { List&lt;String&gt; lst = new ArrayList&lt;String&gt;(); int size = lst.size(); for (int i = 0; i &lt; size; i++) { System.out.println("hi"); } } </code></pre> <hr> <pre><code>public void forLoop1(); Code: 0: new #2; //class java/util/ArrayList 3: dup 4: invokespecial #3; //Method java/util/ArrayList."&lt;init&gt;":()V 7: astore_1 8: iconst_0 9: istore_2 10: iload_2 11: aload_1 12: invokeinterface #4, 1; //InterfaceMethod java/util/List.size:()I 17: if_icmpge 34 20: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream; 23: ldc #6; //String hi 25: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/Str ing;)V 28: iinc 2, 1 31: goto 10 34: return public void forLoop2(); Code: 0: new #2; //class java/util/ArrayList 3: dup 4: invokespecial #3; //Method java/util/ArrayList."&lt;init&gt;":()V 7: astore_1 8: aload_1 9: invokeinterface #4, 1; //InterfaceMethod java/util/List.size:()I 14: istore_2 15: iconst_0 16: istore_3 17: iload_3 18: iload_2 19: if_icmpge 36 22: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream; 25: ldc #6; //String hi 27: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/Str ing;)V 30: iinc 3, 1 33: goto 17 36: return </code></pre> <p>It doesn't optimize for me.</p> <blockquote> <p>java version "1.6.0_22" Java(TM) SE Runtime Environment (build 1.6.0_22-b04) Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)</p> </blockquote> <p>So if you need to choose from mentioned two, go for second, but I personally would go for <code>for-each</code>.</p> <hr> <h1>for-each Performance</h1> <p>From Item 46 in <a href="http://rads.stackoverflow.com/amzn/click/0321356683" rel="noreferrer">Effective Java</a> by Joshua Bloch :</p> <blockquote> <p>The for-each loop, introduced in release 1.5, gets rid of the clutter and the opportunity for error by hiding the iterator or index variable completely. The resulting idiom applies equally to collections and arrays:</p> <pre><code>// The preferred idiom for iterating over collections and arrays for (Element e : elements) { doSomething(e); } </code></pre> <p>When you see the colon (:), read it as “in.” Thus, the loop above reads as “for each element e in elements.” Note that there is no performance penalty for using the for-each loop, even for arrays. In fact, it may offer a slight performance advantage over an ordinary for loop in some circumstances, as it computes the limit of the array index only once. While you can do this by hand (Item 45), programmers don’t always do so.</p> </blockquote> <hr> <h1>See Also</h1> <ul> <li><a href="https://stackoverflow.com/questions/256859/is-there-a-performance-difference-between-a-for-loop-and-a-for-each-loop">Is-there-a-performance-difference-between-a-for-loop-and-a-for-each-loop</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload