Tuesday, December 28, 2010

Comparision between old for loop and Enhanced for loop in Java

Comparision between old for loop and Enhanced for loop in Java


Having decided to try and find out which for loop is better the old-school for loop or the newly enhanced for loop which appeared from JDK 5 onwards. Here are the outcomes of the investigation. All tests run in a laptop with Intel core i5, 3 GB RAM, 2.27 GHz processor.

Running the loop with just one String item


String[] str = {"one"};
for(int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
and
for(String strg: str) {
System.out.println(strg);
}

Resulted in execution time of 0 in both cases.

Increasing the strings in the array from 1 to 5 to 10


resulted in similar times.

Doing a java disassemble on the class files resulted in the following



OldFor
public static void main(java.lang.String[]);
Code:
0: iconst_1
1: anewarray #16; //class java/lang/String
4: dup
5: iconst_0
6: ldc #18; //String one
8: aastore
9: astore_1
10: invokestatic #20; //Method java/lang/System.currentTimeMillis:()J
13: invokestatic #26; //Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
16: astore_2
17: iconst_0
18: istore_3
19: goto 34
22: getstatic #32; //Field java/lang/System.out:Ljava/io/PrintStream;
25: aload_1
26: iload_3
27: aaload
28: invokevirtual #36; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V
31: iinc 3, 1
34: iload_3
35: aload_1
36: arraylength
37: if_icmplt 22
40: invokestatic #20; //Method java/lang/System.currentTimeMillis:()J
43: invokestatic #26; //Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
46: astore_3
47: aload_3
48: invokevirtual #42; //Method java/lang/Long.longValue:()J
51: aload_2
52: invokevirtual #42; //Method java/lang/Long.longValue:()J
55: lsub
56: invokestatic #26; //Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
59: astore 4
61: getstatic #32; //Field java/lang/System.out:Ljava/io/PrintStream;
64: aload 4
66: invokevirtual #45; //Method java/io/PrintStream.println: (Ljava/lang/Object;)V
69: return
}

NewFor

public static void main(java.lang.String[]);
Code:
0: iconst_1
1: anewarray #16; //class java/lang/String
4: dup
5: iconst_0
6: ldc #18; //String one
8: aastore
9: astore_1
10: invokestatic #20; //Method java/lang/System.currentTimeMillis:()J
13: invokestatic #26; //Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
16: astore_2
17: aload_1
18: dup
19: astore 6
21: arraylength
22: istore 5
24: iconst_0
25: istore 4
27: goto 46
30: aload 6
32: iload 4
34: aaload
35: astore_3
36: getstatic #32; //Field java/lang/System.out:Ljava/io/PrintStream;
39: aload_3
40: invokevirtual #36; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V
43: iinc 4, 1
46: iload 4
48: iload 5
50: if_icmplt 30
53: invokestatic #20; //Method java/lang/System.currentTimeMillis:()J
56: invokestatic #26; //Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
59: astore_3
60: aload_3
61: invokevirtual #42; //Method java/lang/Long.longValue:()J
64: aload_2
65: invokevirtual #42; //Method java/lang/Long.longValue:()J
68: lsub
69: invokestatic #26; //Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
72: astore 4
74: getstatic #32; //Field java/lang/System.out:Ljava/io/PrintStream;
77: aload 4
79: invokevirtual #45; //Method java/io/PrintStream.println:(Ljava/lang/Ob
ject;)V
82: return
}

The total number of lines in the old for was 37 and in the new for was 42.

Finally


End Result With respect to time both for's perform equally well, Examining after disassembling results in the enhanced for being longer as there is one more layer of abstraction in the Enhanced For.

Final analysis Use any of the for's to achieve looping.