abstract class Benchmark { abstract void benchmark(); public final long repeat(int count) { long start = System.currentTimeMillis(); for (int i=0; i < count; i++) benchmark(); return(System.currentTimeMillis() - start); } } class LoopBenchmark extends Benchmark { long count=0; LoopBenchmark(int c) { count = c; } void benchmark() { for (int i=0; i < count; i++); } } class FactBenchmark extends Benchmark { int count=0; FactBenchmark(int c) { count = c; } int Fact(int n) { return((n <= 1)? 1 : Fact(n-1)*n); } void benchmark() { int f = Fact(count); } } class MethodBenchmark extends Benchmark { void benchmark() { } public static void main(String[] args) { int count = Integer.parseInt(args[0]); long time = new MethodBenchmark().repeat(count); System.out.println(count + " methods in " + time + " milliseconds"); time = new LoopBenchmark(1000000).repeat(count); System.out.println("Loop(1000000) " + count + " methods in " + time + " milliseconds"); time = new FactBenchmark(10000).repeat(count); System.out.println("Fact(10000) " + count + " methods in " + time + " milliseconds"); } }