Created
May 27, 2016 16:09
-
-
Save isopov/acdd4c2820982a08718533d7f0a2d1c2 to your computer and use it in GitHub Desktop.
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.lang.reflect.Field; | |
import java.util.concurrent.TimeUnit; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Measurement; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.Warmup; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.RunnerException; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import com.esotericsoftware.reflectasm.FieldAccess; | |
@State(Scope.Benchmark) | |
@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS) | |
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | |
@Fork(1) | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
public class ReflectAsmBenchmark { | |
private FooBar fooBar = new FooBar(); | |
private Field field; | |
private FieldAccess fieldAcccess = FieldAccess.get(FooBar.class); | |
private int fieldIndex = fieldAcccess.getIndex("value"); | |
@Setup | |
public void setup() throws Exception { | |
field = FooBar.class.getField("value"); | |
field.setAccessible(true); | |
} | |
@Benchmark | |
public Object testReflection() throws IllegalArgumentException, IllegalAccessException { | |
return field.get(fooBar); | |
} | |
@Benchmark | |
public Object testReflectasm() throws IllegalArgumentException, IllegalAccessException { | |
return fieldAcccess.get(fooBar, fieldIndex); | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder().include(ReflectAsmBenchmark.class.getSimpleName()).build(); | |
new Runner(opt).run(); | |
} | |
public static class FooBar { | |
public String value = "foo"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment