1 package test1; 2 3 public class BenchStaticMethod { 4 public static final int N = 10000000; 5 foo(int i)6 public static int foo(int i) { 7 i /= 100000; 8 int f = 1; 9 while (i > 1) 10 f *= i--; 11 12 return f; 13 } 14 foo2(int i)15 public static void foo2(int i) {} 16 17 public static int num = 0; 18 test()19 public static int test() { 20 long time = System.currentTimeMillis(); 21 for (int i = N; i > 0; --i) 22 foo(i); 23 24 long time2 = System.currentTimeMillis(); 25 return (int)(time2 - time); 26 } 27 orgTest()28 public static int orgTest() { 29 long time = System.currentTimeMillis(); 30 for (int i = N; i > 0; --i) 31 foo(i); 32 33 long time2 = System.currentTimeMillis(); 34 return (int)(time2 - time); 35 } 36 handTest()37 public static int handTest() { 38 long time = System.currentTimeMillis(); 39 for (int i = N; i > 0; --i) { 40 num += i; 41 foo(i); 42 } 43 44 long time2 = System.currentTimeMillis(); 45 return (int)(time2 - time); 46 } 47 main(String[] args)48 public static void main(String[] args) throws Exception { 49 System.out.println("orgTest (msec) " + orgTest()); 50 System.out.println("handTest (msec) " + handTest()); 51 System.out.println("test (msec) " + test()); 52 } 53 } 54