分类: LINUX
2008-11-18 10:40:52
Note that javac -O can generate illegal code by inlining things that it shouldn't (tip from ). Running Geoff's code through Visual Cafe and Visual J++ shows that Symantec has the same problem, but Microsoft doesn't. See also the excellent from .
Also, tools such as preEmptive's will now do most of these missing optimizations for you.
for (i = 0; i < a.length; i++) { float tmp = c * d; b[i] = a[i] + c * d; for (i = 0; i < a.length; i++) { } b[i] = a[i] + tmp; } 6 aload_2 12 aload_2 7 iload 5 13 iload 6 9 aload_1 15 aload_1 10 iload 5 16 iload 6 12 faload 18 faload 13 fload_3 19 fload 5 14 fload 4 21 fadd 16 fmul 22 fastore 17 fadd 23 iinc 6 1 18 fastore 26 iload 6 19 iinc 5 1 28 aload_1 22 iload 5 29 arraylength 24 aload_1 30 if_icmplt 12 25 arraylength 26 if_icmplt 6
double d = a * Math.sqrt (c); double tmp = Math.sqrt (c); double e = b * Math.sqrt (c); double d = a * tmp; double e = b * tmp; 0 dload_1 0 dload 5 1 dload 5 2 invokestatic #5
for (i = 0; i < a.length; i++) { for (i = 0; i < a.length; i++) { a[i] = a[i] + x; a[i] += x; } } 5 aload_1 5 aload_1 6 iload_3 6 iload_3 7 aload_1 7 dup2 8 iload_3 8 iaload 9 iaload 9 iload_2 10 iload_2 10 iadd 11 iadd 11 iastore 12 iastore 12 iinc 3 1 13 iinc 3 1 15 iload_3 16 iload_3 16 aload_1 17 aload_1 17 arraylength 18 arraylength 18 if_icmplt 5 19 if_icmplt 5Adding extra reference variables to allow the compiler to eliminate opcodes or use faster bytecodes can also be considered a case of strength reduction. For example, if you're repeatedly accessing elements in a single row of a 2D array, make a 1D array variable that points to that row. See class dgefa in for an example of this optimization -- it saves you an iload and an aaload on each reference.
Similarly, if you have a subclass that performs lots of operations on an object defined in its superclass, making a reference to the object in the subclass with super and then using that reference directly will enable the compiler to replace getfield opcodes with aload opcodes (tip from ).
int[] a = (int[]) pop (); int i; int[] b = (int[]) pop (); int[] a = (int[]) pop (); int[] dst = new int[a.length]; int[] b = (int[]) pop (); int i; int[] dst = new int[a.length]; for (i = 0; i < a.length; i++) { for (i = 0; i < a.length; i++) { dst[i] = a[i] + b[i]; dst[i] = a[i] + b[i]; } } 27 aload_3 27 aload 4 28 iload 4 29 iload_1 30 aload_1 30 aload_2 31 iload 4 31 iload_1 33 iaload 32 iaload 34 aload_2 33 aload_3 35 iload 4 34 iload_1 37 iaload 35 iaload 38 iadd 36 iadd 39 iastore 37 iastore 40 iinc 4 1 38 iinc 1 1 43 iload 4 41 iload_1 45 aload_1 42 aload_2 46 arraylength 43 arraylength 47 if_icmplt 27 44 if_icmplt 27