1 8 package test; 9 10 13 public class CastBench implements Cast { 14 private static final int NR_INVOCATIONS = 1000000000; 15 16 public void invoke() { 17 } 18 19 public void invokeInterface() { 20 } 21 22 public static void main(String [] args) { 23 CastBench bench = new CastBench(); 24 for (int i = 0; i < NR_INVOCATIONS; i++) { 25 bench.invoke(); 26 } 27 28 benchRegularInvoke(bench); 29 benchInterfaceInvoke(bench); 30 benchCastInvoke(bench); 31 } 32 33 private static void benchCastInvoke(CastBench bench) { 34 long start = System.currentTimeMillis(); 35 for (int i = 0; i < NR_INVOCATIONS; i++) { 36 ((Cast) bench).invokeInterface(); 37 } 38 long end = System.currentTimeMillis() - start; 39 double time = end / (double) NR_INVOCATIONS; 40 System.out.println("cast invoke = " + time); 41 } 42 43 private static void benchInterfaceInvoke(Cast bench) { 44 long start = System.currentTimeMillis(); 45 for (int i = 0; i < NR_INVOCATIONS; i++) { 46 bench.invokeInterface(); 47 } 48 long end = System.currentTimeMillis() - start; 49 double time = end / (double) NR_INVOCATIONS; 50 System.out.println("interface invoke = " + time); 51 } 52 53 private static void benchRegularInvoke(CastBench bench) { 54 long start = System.currentTimeMillis(); 55 for (int i = 0; i < NR_INVOCATIONS; i++) { 56 bench.invoke(); 57 } 58 long end = System.currentTimeMillis() - start; 59 double time = end / (double) NR_INVOCATIONS; 60 System.out.println("regular invoke = " + time); 61 } 62 } 63 | Popular Tags |