Created
February 2, 2018 12:28
-
-
Save headius/9c28c240ea978617951c0ad46933aeb4 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
private static final Method nop1; | |
private static final MethodHandle nop2; | |
private static final MethodHandle nop3; | |
static { | |
Method _nop1 = null; | |
MethodHandle _nop2 = null; | |
MethodHandle _nop3 = null; | |
try { | |
_nop1 = Hello.class.getMethod("nop", long.class); | |
_nop2 = lookup.unreflect(_nop1); | |
_nop3 = MethodHandles.insertArguments(_nop2, 0, 1L); | |
} catch (Exception e) {} | |
nop1 = _nop1; | |
nop2 = _nop2; | |
nop3 = _nop3; | |
} | |
public static void main(String[] args) throws Throwable { | |
MethodHandle nop2 = lookup.unreflect(nop1); | |
while (true) { | |
long t = System.nanoTime(); | |
for (int i = 0; i < 100_000_000; i++) { | |
nop2.invokeExact(1L); | |
} | |
System.out.println("invoke: " + (System.nanoTime() - t)); | |
// long t = System.nanoTime(); | |
// for (int i = 0; i < 100_000_000; i++) { | |
// nop1.invoke(null, 1L); | |
// } | |
// System.out.println("reflect: " + (System.nanoTime() - t)); | |
// long t = System.nanoTime(); | |
// for (int i = 0; i < 100_000_000; i++) { | |
// nop3.invoke(); | |
// } | |
// System.out.println("invoke2: " + (System.nanoTime() - t)); | |
// long t = System.nanoTime(); | |
// for (int i = 0; i < 100_000_000; i++) { | |
// nop(1L); | |
// } | |
// System.out.println("direct: " + (System.nanoTime() - t)); | |
} | |
} | |
private static long counter = 0; | |
public static void nop(long inc) { | |
counter += inc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment