2009年5月25日月曜日

timeit on java

SpringのAutoWireの有無による速度差と、CGLIBとProxyの速度差を調べたくなった。
AutoWireの有無による速度差はなし。
CGLIBとProxyも十分無視してよい速度差。

上の目的のためにtimeitみたいに、適当な回数分だけループして時間を計測してくれるツールを作った。
一定の時間を越えるまで回数を増やしてループし続けます。
最初は500msにしたら、テストスイートが終わらなくなったので、50ms。
比較用に交互に実行する機能があるといいかも。
  1. public abstract class Timeit {  
  2.  protected String name;  
  3.   
  4.  public Timeit(String name) {  
  5.   this.name = name;  
  6.  }  
  7.   
  8.  public void timeit() {  
  9.   int before = 0;  
  10.   int count = 1;  
  11.   long sum = 0;  
  12.   prepare();  
  13.   while (true) {  
  14.    int loop = count - before;  
  15.    long start = System.nanoTime();  
  16.    for (int i = 0; i < loop; ++i) {  
  17.     this.invoke();  
  18.    }  
  19.    long end = System.nanoTime();  
  20.    long current = end - start;  
  21.    sum += current;  
  22.    if (current > 50 * 1000 * 1000) {  
  23.     break;  
  24.    }  
  25.    before = count;  
  26.    count = count * 2;  
  27.   }  
  28.   long time = sum / count;  
  29.   System.out.print(name + ": ");  
  30.   if (time < 1000) {  
  31.    System.out.printf("%dns\n", time);  
  32.   } else if (time < 1000 * 1000) {  
  33.    System.out.printf("%.3fμs %dtimes\n", time / 1000.0, count);  
  34.   } else if (time < 1000 * 1000 * 1000) {  
  35.    System.out.printf("%.3fms %dtimes\n", time / 1000.0 / 1000, count);  
  36.   } else {  
  37.    System.out.printf("%.3fs %dtimes\n", time / 1000.0 / 1000 / 1000,  
  38.      count);  
  39.   }  
  40.  }  
  41.   
  42.  public void prepare() {  
  43.   
  44.  }  
  45.   
  46.  public abstract void invoke();  
  47.   
  48.  public static class TimeItTest extends TestCase {  
  49.   public void testSimple() throws Exception {  
  50.    new Timeit("simple") {  
  51.     public void invoke() {  
  52.      try {  
  53.       Thread.sleep(2);  
  54.      } catch (InterruptedException e) {  
  55.      }  
  56.     }  
  57.    }.timeit();  
  58.   }  
  59.  }  
  60. }  

0 件のコメント: