-
-
Save anonymous/32e698d04c7456c10f069686072d7cc6 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
Benchmark Mode Samples Score Error Units | |
d.t.MyBenchmark.invoke avgt 5 10,150 ▒ 0,352 ns/op | |
d.t.MyBenchmark.invokeExact avgt 5 10,262 ▒ 0,841 ns/op | |
d.t.MyBenchmark.invokeWithArguments avgt 5 176,235 ▒ 2,148 ns/op | |
d.t.MyBenchmark.reflective avgt 5 13,569 ▒ 0,266 ns/op | |
d.t.MyBenchmark.staticInvoke avgt 5 5,481 ▒ 0,101 ns/op | |
d.t.MyBenchmark.staticInvokeExact avgt 5 5,497 ▒ 0,146 ns/op | |
d.t.MyBenchmark.staticInvokeWithArguments avgt 5 185,668 ▒ 1,766 ns/op |
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
package dk.test; | |
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.Threads; | |
import org.openjdk.jmh.annotations.Warmup; | |
import java.lang.invoke.MethodHandle; | |
import java.lang.invoke.MethodHandles; | |
import java.lang.invoke.MethodType; | |
import java.lang.reflect.Constructor; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.concurrent.ThreadLocalRandom; | |
import java.util.concurrent.TimeUnit; | |
@Threads(1) | |
@Fork(value = 1) | |
@Warmup(iterations = 5, time = 1) | |
@Measurement(iterations = 5, time = 3) | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
public class MyBenchmark { | |
static final MethodHandle staticMh = getMh(); | |
static MethodHandle getMh() { | |
try { | |
return MethodHandles.lookup().findConstructor(MyClass.class, MethodType.methodType(void.class, int.class, String.class, double.class, List.class)); | |
} | |
catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
static class MyClass { | |
private final int i; | |
private final String str; | |
private final double d; | |
private final List<?> list; | |
MyClass(int i, String str, double d, List<?> list) { | |
this.i = i; | |
this.str = str; | |
this.d = d; | |
this.list = list; | |
} | |
} | |
@State(Scope.Thread) | |
public static class StateClass { | |
int i; | |
String str; | |
double d; | |
List<?> list; | |
Object[] args; | |
Constructor<MyClass> cons; | |
MethodHandle mh; | |
@Setup | |
public void setup() throws Throwable { | |
i = ThreadLocalRandom.current().nextInt(); | |
d = Math.random(); | |
list = new ArrayList<Number>(Arrays.asList(i, d)); | |
str = list.toString(); | |
args = new Object[] {i, str, d, list}; | |
cons = (Constructor<MyClass>) MyClass.class.getDeclaredConstructors()[0]; | |
mh = MethodHandles.lookup().findConstructor(MyClass.class, MethodType.methodType(void.class, int.class, String.class, double.class, List.class)); | |
} | |
} | |
@Benchmark | |
public MyClass reflective(StateClass state) throws Throwable { | |
return state.cons.newInstance(state.args); | |
} | |
@Benchmark | |
public MyClass invokeWithArguments(StateClass state) throws Throwable { | |
return (MyClass)state.mh.invokeWithArguments(state.args); | |
} | |
@Benchmark | |
public MyClass invokeExact(StateClass state) throws Throwable { | |
return (MyClass)state.mh.invokeExact(state.i, state.str, state.d, state.list); | |
} | |
@Benchmark | |
public MyClass invoke(StateClass state) throws Throwable { | |
return (MyClass)state.mh.invoke(state.i, state.str, state.d, state.list); | |
} | |
@Benchmark | |
public MyClass staticInvokeWithArguments(StateClass state) throws Throwable { | |
return (MyClass)staticMh.invokeWithArguments(state.args); | |
} | |
@Benchmark | |
public MyClass staticInvokeExact(StateClass state) throws Throwable { | |
return (MyClass)staticMh.invokeExact(state.i, state.str, state.d, state.list); | |
} | |
@Benchmark | |
public MyClass staticInvoke(StateClass state) throws Throwable { | |
return (MyClass)staticMh.invoke(state.i, state.str, state.d, state.list); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment