Is there a way to assess/measure the efficiency of code in java -
for instance, have 2 versions of simple count loop - both achieve same thing 1 more efficient (and presumably uses less memory) other. see below:
code 1:
int num; (num =0; num<10; num++){ system.out.println(num); }
code 2:
for (int num=0; num<10; num++){ system.out.println(num); }
they compile identically , have identical performance , memory requirements.
for proof @ compiled java byte code using javap -c <classfile>
code:
public static void a() { (int num = 0; num < 10; num++) { system.out.println(num); } } public static void b() { int num; (num = 0; num < 10; num++) { system.out.println(num); } }
byte code:
public static void a(); code: 0: iconst_0 1: istore_0 2: goto 15 5: getstatic #15 // field java/lang/system.out:ljava/io/printstream; 8: iload_0 9: invokevirtual #21 // method java/io/printstream.println:(i)v 12: iinc 0, 1 15: iload_0 16: bipush 10 18: if_icmplt 5 21: return public static void b(); code: 0: iconst_0 1: istore_0 2: goto 15 5: getstatic #15 // field java/lang/system.out:ljava/io/printstream; 8: iload_0 9: invokevirtual #21 // method java/io/printstream.println:(i)v 12: iinc 0, 1 15: iload_0 16: bipush 10 18: if_icmplt 5 21: return
Comments
Post a Comment