Created
June 3, 2014 22:25
-
-
Save cberzan/46a5f988ad7e3ec5273c to your computer and use it in GitHub Desktop.
benchmark cost of checking boolean flags on every call
This file contains 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
import java.util.Random; | |
public class Benchmark { | |
public Benchmark() { | |
rng = new Random(); | |
hasLower = false; | |
hasUpper = false; | |
} | |
public void setParams(Double lower, Double upper) { | |
if (lower != null) { | |
this.lower = lower; | |
this.hasLower = true; | |
} | |
if (upper != null) { | |
this.upper = upper; | |
this.hasUpper = true; | |
} | |
if (this.hasLower && this.hasUpper && this.lower >= this.upper) { | |
throw new IllegalArgumentException("lower >= upper"); | |
} | |
} | |
private void checkHasParams() { | |
if (!hasLower) { | |
throw new IllegalArgumentException("lower not provided"); | |
} | |
if (!hasUpper) { | |
throw new IllegalArgumentException("upper not provided"); | |
} | |
} | |
public double sampleWithoutCheck() { | |
return lower + (rng.nextDouble() * (upper - lower)); | |
} | |
public double sampleWithCheck() { | |
checkHasParams(); | |
return lower + (rng.nextDouble() * (upper - lower)); | |
} | |
public double getLogProbWithCheck(double value) { | |
checkHasParams(); | |
if ((value >= lower) && (value < upper)) { | |
return Math.log(1.0 / (upper - lower)); | |
} else { | |
return Double.NEGATIVE_INFINITY; | |
} | |
} | |
public double getLogProbWithoutCheck(double value) { | |
if ((value >= lower) && (value < upper)) { | |
return Math.log(1.0 / (upper - lower)); | |
} else { | |
return Double.NEGATIVE_INFINITY; | |
} | |
} | |
private Random rng; | |
private boolean hasLower; | |
private boolean hasUpper; | |
private double lower; | |
private double upper; | |
///////////////////////////////////////////////////////////////////////////// | |
public static void main(String[] args) { | |
Benchmark b = new Benchmark(); | |
b.setParams(5.0, 6.0); | |
// Warm up the jit. | |
final int numWarmupTrials = 1000000; | |
for (int i = 0; i < numWarmupTrials; i++) { | |
b.sampleWithoutCheck(); | |
b.sampleWithCheck(); | |
b.getLogProbWithoutCheck(5.5); | |
b.getLogProbWithCheck(5.5); | |
} | |
// Now benchmark. | |
final int numTrials = 10000000; | |
long sampleWithoutCheckNanos = System.nanoTime(); | |
for (int i = 0; i < numTrials; i++) { | |
b.sampleWithoutCheck(); | |
} | |
sampleWithoutCheckNanos = System.nanoTime() - sampleWithoutCheckNanos; | |
System.out.println("sampleWithoutCheck: " + | |
sampleWithoutCheckNanos / 1e9 / numTrials + " seconds"); | |
long sampleWithCheckNanos = System.nanoTime(); | |
for (int i = 0; i < numTrials; i++) { | |
b.sampleWithCheck(); | |
} | |
sampleWithCheckNanos = System.nanoTime() - sampleWithCheckNanos; | |
System.out.println("sampleWithCheck: " + | |
sampleWithCheckNanos / 1e9 / numTrials + " seconds"); | |
long getLogProbWithoutCheckNanos = System.nanoTime(); | |
for (int i = 0; i < numTrials; i++) { | |
b.getLogProbWithoutCheck(5.5); | |
} | |
getLogProbWithoutCheckNanos = System.nanoTime() - getLogProbWithoutCheckNanos; | |
System.out.println("getLogProbWithoutCheck: " + | |
getLogProbWithoutCheckNanos / 1e9 / numTrials + " seconds"); | |
long getLogProbWithCheckNanos = System.nanoTime(); | |
for (int i = 0; i < numTrials; i++) { | |
b.getLogProbWithCheck(5.5); | |
} | |
getLogProbWithCheckNanos = System.nanoTime() - getLogProbWithCheckNanos; | |
System.out.println("getLogProbWithCheck: " + | |
getLogProbWithCheckNanos / 1e9 / numTrials + " seconds"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment