Skip to content

Instantly share code, notes, and snippets.

@zserge
Created October 5, 2014 15:04
Show Gist options
  • Save zserge/c1c61ecf872b6bae93a6 to your computer and use it in GitHub Desktop.
Save zserge/c1c61ecf872b6bae93a6 to your computer and use it in GitHub Desktop.
Tiny benchmark class
/** A tiny benchmark class */
public static class B {
private int count = 0;
private long start;
private long end;
private long min = Long.MAX_VALUE;
private long max;
private String label;
public B(String label) {
this.label = label;
}
public boolean done() {
long t = System.nanoTime();
if (this.count == 0) {
this.start = this.end = t;
} else {
long d = t - this.end;
if (d < this.min) {
this.min = d;
}
if (d > this.max) {
this.max = d;
}
}
this.end = t;
this.count++;
return this.end - this.start >= 5*1e9 || count >= 1000000;
}
public String report() {
return "Benchmark " + this.label + "\t\t" + this.ops() + "\t" + (this.op() / 1000000) +
"ms/op, min=" + (this.min/1000000) + ", max=" + (this.max/1000000);
}
public int ops() {
return this.count - 1;
}
public double op() {
return this.time()/this.ops();
}
public long time() {
return (this.end - this.start);
}
}
@zserge
Copy link
Author

zserge commented Oct 5, 2014

Usage:

B b = new B("My benchmark");
while (!b.done()) {
...
}
System.out.println(b.report());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment