Skip to content

Instantly share code, notes, and snippets.

@matriv
Created March 19, 2017 10:40
Show Gist options
  • Save matriv/2b1ba8f006294128e8a9c36744e02b74 to your computer and use it in GitHub Desktop.
Save matriv/2b1ba8f006294128e8a9c36744e02b74 to your computer and use it in GitHub Desktop.
Benchmark Mode Cnt Score Error Units
MyBenchmark.testReady thrpt 200 65138596.553 ± 559898.434 ops/s
MyBenchmark.testReflection thrpt 200 837419.902 ± 20471.042 ops/s
public class MyBenchmark {
private static final String routingHashFunctionClass = "com.jenkov.DjbHashFunction";
private static Method method;
static {
try {
method = Class.forName(routingHashFunctionClass).getDeclaredMethod("hash", String.class);
} catch (NoSuchMethodException | ClassNotFoundException e) {
e.printStackTrace();
}
}
@Benchmark
public void testReflection(Blackhole blackhole) {
try {
Class<?> hashFunctionClass = Class.forName(routingHashFunctionClass);
blackhole.consume(hashFunctionClass.getDeclaredMethod("hash", String.class).invoke(null,"test"));
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | ClassNotFoundException e) {
e.printStackTrace();
}
}
@Benchmark
public void testReady(Blackhole blackhole) {
try {
blackhole.consume(method.invoke(null,"test"));
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment