DVL1 dishevelled.org

Skip to: [content] [navigation]

dishevelled.org timer 1.1-SNAPSHOT :: Downloads | Repository | Javadocs | Checkstyle | Test coverage | Changes


dishevelled.org timer provides a timer class with nanosecond resolution and summary statistics on recorded elapsed times. The timer project requires java 1.5 or later.


Timer t = new Timer();
for (int i = 0; i < 100; i++) {
  // ... the code being measured ...

System.out.println("timer results, n=" + t.size());
System.out.println("  mean=" + t.mean() + " stdev=" + t.standardDeviation());
System.out.println("  min=" + t.min() + " max=" + t.max());

The recorded times themselves are not preserved, however, only the summary statistics. As a consequence start() and stop() can be called many number of times without requiring large amounts of memory.


Execution time can be sensitive to various factors, such as order of execution, runtime optimization from the just-in-time compiler (JIT), and garbage collection. This library provides static methods on Timer to help deal with these factors.

Given a few benchmarks to run, wrap them in Runnable objects

Runnable r0 = new Runnable() { public void run() { ... } };
Runnable r1 = new Runnable() { public void run() { ... } };
Runnable r2 = new Runnable() { public void run() { ... } };
List<Runnable> benchmarks = Arrays.asList(new Runnable[] { r0, r1, r2 });

Prime the JIT by running the benchmarks several times

Timer.prime(benchmarks, 1000);

Then measure the execution times of the benchmarks by running them several times in random execution order

Map<Runnable, Timer> result = Timer.shuffle(benchmarks, 100, 100);

Summary statistics on recorded execution times are captured by the timer returned for each Runnable benchmark

for (Map.Entry<Runnable, Timer> e : result.entrySet()) {
  Runnable r = e.getKey();
  Timer t = e.getValue();
  System.out.println("runnable=" + r + " mean execution time=" + t.mean() + "ns");


SourceForge.net Logo