Java Reference
In-Depth Information
}
}
A
ny class with any
abstract
methods must be declared
abstract
. This re-
dundancy helps the reader quickly see that the class is
abstract
without
scanning to see whether any method in the class is declared
abstract
.
The
repeat
method provides the benchmarking expertise. It can time a
run of
count
repetitions of the benchmark. The method
System.nanoTime
starting time from the finishing time you get an approximation of the
time spent executing the benchmark. If the timing needs become more
complex (perhaps measuring the time of each run and computing stat-
istics about the variations), this method can be enhanced without affect-
ing any extended class's implementation of its specialized benchmark
code.
The
abstract
method
benchmark
must be implemented by each subclass
that is not
abstract
itself. This is why it has no implementation in this
class, just a declaration. Here is an example of a simple
Benchmark
ex-
tension:
class MethodBenchmark extends Benchmark {
/** Do nothing, just return. */
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 + " nanoseconds");
}
}