2009年5月25日月曜日

timeit on java

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

上の目的のためにtimeitみたいに、適当な回数分だけループして時間を計測してくれるツールを作った。
一定の時間を越えるまで回数を増やしてループし続けます。
最初は500msにしたら、テストスイートが終わらなくなったので、50ms。
比較用に交互に実行する機能があるといいかも。

public abstract class Timeit {
protected String name;

public Timeit(String name) {
this.name = name;
}

public void timeit() {
int before = 0;
int count = 1;
long sum = 0;
prepare();
while (true) {
int loop = count - before;
long start = System.nanoTime();
for (int i = 0; i < loop; ++i) {
this.invoke();
}
long end = System.nanoTime();
long current = end - start;
sum += current;
if (current > 50 * 1000 * 1000) {
break;
}
before = count;
count = count * 2;
}
long time = sum / count;
System.out.print(name + ": ");
if (time < 1000) {
System.out.printf("%dns\n", time);
} else if (time < 1000 * 1000) {
System.out.printf("%.3fμs %dtimes\n", time / 1000.0, count);
} else if (time < 1000 * 1000 * 1000) {
System.out.printf("%.3fms %dtimes\n", time / 1000.0 / 1000, count);
} else {
System.out.printf("%.3fs %dtimes\n", time / 1000.0 / 1000 / 1000,
count);
}
}

public void prepare() {

}

public abstract void invoke();

public static class TimeItTest extends TestCase {
public void testSimple() throws Exception {
new Timeit("simple") {
public void invoke() {
try {
Thread.sleep(2);
} catch (InterruptedException e) {
}
}
}.timeit();
}
}
}

0 件のコメント: