Created
October 5, 2014 15:04
-
-
Save zserge/c1c61ecf872b6bae93a6 to your computer and use it in GitHub Desktop.
Tiny benchmark class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: