Skip to content

Instantly share code, notes, and snippets.

@spullara
Created May 5, 2012 22:52
Show Gist options
  • Save spullara/2606170 to your computer and use it in GitHub Desktop.
Save spullara/2606170 to your computer and use it in GitHub Desktop.
Benchmarking some of the features in JDK8
package jdk8tests;
import java.util.functions.IntBinaryOperator;
import java.util.functions.LongBinaryOperator;
/**
* Do we really need both Int and Long versions?
*/
public class IntVsLong {
static void test(Runnable bench, int times) {
long start = System.nanoTime();
for (int i = 0; i < times; i++) {
bench.run();
}
long end = System.nanoTime();
System.out.println((end - start)/times + "ns per run");
}
public static void main(String[] args) {
final IntBinaryOperator ibo1 = new IntBinaryOperator() {
@Override
public int eval(int left, int right) {
return (left + right) * left * right / (left - right);
}
};
final IntBinaryOperator ibo2 = (left, right) -> { return (left + right) * left * right / (left - right); };
final LongBinaryOperator lbo1 = new LongBinaryOperator() {
@Override
public long eval(long left, long right) {
return (left + right) * left * right / (left - right);
}
};
final LongBinaryOperator lbo2 = (left, right) -> { return (left + right) * left * right / (left - right); };
Runnable i1 = new Runnable() {
@Override
public void run() {
ibo1.eval(1000, 2000);
}
};
Runnable i2 = () -> { ibo2.eval(1000, 2000); };
Runnable l1 = new Runnable() {
@Override
public void run() {
lbo1.eval(1000, 2000);
}
};
Runnable l2 = () -> { lbo2.eval(1000, 2000); };
test(i1, 10000);
test(i2, 10000);
test(l1, 10000);
test(l2, 10000);
test(i1, 1000000000);
test(i2, 10000000);
test(l1, 1000000000);
test(l2, 10000000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment