Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sun.reflect.noInflation makes code faster?
import org.openjdk.jmh.annotations.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
@Fork(3)
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class MethodCallTest {
@Param({"true", "false"})
private boolean accessible;
@Param({"true", "false"})
private boolean polymorph;
private int i;
Method method1, method2, method3;
Person p;
public static class Person {
private String name;
Person(String name) {
this.name = name;
}
public String getName1() {
return name;
}
public String getName2() {
return name;
}
public String getName3() {
return name;
}
}
@Setup
public void setup() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
method1 = Person.class.getMethod("getName1");
method1.setAccessible(accessible);
method2 = Person.class.getMethod("getName2");
method2.setAccessible(accessible);
method3 = Person.class.getMethod("getName3");
method3.setAccessible(accessible);
p = new Person(String.valueOf(Math.random()));
if(polymorph) {
for(int i=0; i<1000; i++) {
method2.invoke(p);
method3.invoke(p);
}
}
}
@Benchmark
public String reflect() throws InvocationTargetException, IllegalAccessException {
return (String) method1.invoke(p);
}
}
# JMH 1.17 (released 33 days ago)
# VM version: JDK 1.8.0_92, VM 25.92-b14
# VM invoker: C:\Program Files\Java\jre1.8.0_92\bin\java.exe
# VM options: <none>
Benchmark (accessible) (polymorph) Mode Cnt Score Error Units
MethodCallTest.reflect true true avgt 30 8,216 ± 0,061 ns/op
MethodCallTest.reflect true false avgt 30 6,602 ± 0,024 ns/op
MethodCallTest.reflect false true avgt 30 9,432 ± 0,123 ns/op
MethodCallTest.reflect false false avgt 30 7,589 ± 0,031 ns/op
# JMH 1.17 (released 33 days ago)
# VM version: JDK 1.8.0_92, VM 25.92-b14
# VM invoker: C:\Program Files\Java\jre1.8.0_92\bin\java.exe
# VM options: -Dsun.reflect.noInflation=true
Benchmark (accessible) (polymorph) Mode Cnt Score Error Units
MethodCallTest.reflect true true avgt 30 7,722 ± 0,089 ns/op
MethodCallTest.reflect true false avgt 30 5,724 ± 0,035 ns/op
MethodCallTest.reflect false true avgt 30 9,043 ± 0,103 ns/op
MethodCallTest.reflect false false avgt 30 7,432 ± 0,102 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.