Created
February 17, 2024 07:03
-
-
Save XiaoPangxie732/d5af8e3c110b1f21ec0e513ea3981916 to your computer and use it in GitHub Desktop.
random benchmarks
This file contains hidden or 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
@State(Scope.Thread) | |
@Warmup(iterations = 3) | |
@Measurement(iterations = 3) | |
@Fork(1) | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
public class InlineBenchmark { | |
private Test test; | |
private static final MethodHandle HANDLE; | |
private static final Method METHOD; | |
private static final Function<Test, String> FUNC = Test::call; | |
static { | |
try { | |
HANDLE = MethodHandles.lookup().findVirtual(Test.class, "call", MethodType.methodType(String.class)); | |
var m = Test.class.getMethod("call"); | |
m.setAccessible(true); | |
METHOD = m; | |
} catch (NoSuchMethodException | IllegalAccessException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static class Test { | |
private String s = "hello"; | |
public String call() { | |
return s; | |
} | |
} | |
private final MethodHandle handle; | |
private MethodHandle handleNotFinal; | |
private final Function<Test, String> func; | |
private final Function<Test, String> func2; | |
private final Method method; | |
private Method methodNotFinal; | |
private final Function<Test, String> func3; | |
private final Function<Test, String> func4; | |
public InlineBenchmark() { | |
try { | |
var mh = MethodHandles.lookup().findVirtual(Test.class, "call", MethodType.methodType(String.class)); | |
handle = mh; | |
handleNotFinal = mh; | |
func = obj -> { | |
try { | |
return (String) mh.invokeExact(obj); | |
} catch (Throwable e) { | |
throw new RuntimeException(e); | |
} | |
}; | |
func2 = obj -> { | |
try { | |
return (String) handle.invokeExact(obj); | |
} catch (Throwable e) { | |
throw new RuntimeException(e); | |
} | |
}; | |
var m = Test.class.getMethod("call"); | |
m.setAccessible(true); | |
method = m; | |
methodNotFinal = m; | |
func3 = obj -> { | |
try { | |
return (String) m.invoke(obj); | |
} catch (Throwable e) { | |
throw new RuntimeException(e); | |
} | |
}; | |
func4 = obj -> { | |
try { | |
return (String) method.invoke(obj); | |
} catch (Throwable e) { | |
throw new RuntimeException(e); | |
} | |
}; | |
} catch (NoSuchMethodException | IllegalAccessException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
@Setup | |
public void setup() { | |
test = new Test(); | |
} | |
@Benchmark | |
public String direct() { | |
return test.call(); | |
} | |
@Benchmark | |
public String methodHandleStatic() throws Throwable { | |
return (String) HANDLE.invokeExact(test); | |
} | |
@Benchmark | |
public String reflectionStatic() throws Throwable { | |
return (String) METHOD.invoke(test); | |
} | |
@Benchmark | |
public String lambdaDirect() { | |
return FUNC.apply(test); | |
} | |
@Benchmark | |
public String methodHandleNotFinal() throws Throwable { | |
return (String) handleNotFinal.invokeExact(test); | |
} | |
@Benchmark | |
public String methodHandle() throws Throwable { | |
return (String) handle.invokeExact(test); | |
} | |
@Benchmark | |
public String methodHandleLambdaWrapped() { | |
return func.apply(test); | |
} | |
@Benchmark | |
public String methodHandleLambdaWrappedWithThis() { | |
return func2.apply(test); | |
} | |
@Benchmark | |
public String reflectionNotFinal() throws Throwable { | |
return (String) methodNotFinal.invoke(test); | |
} | |
@Benchmark | |
public String reflection() throws Throwable { | |
return (String) method.invoke(test); | |
} | |
@Benchmark | |
public String reflectionLambdaWrapped() { | |
return func3.apply(test); | |
} | |
@Benchmark | |
public String reflectionLambdaWrappedWithThis() { | |
return func4.apply(test); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment