Skip to content

Instantly share code, notes, and snippets.

@headius
Created February 2, 2018 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save headius/9c28c240ea978617951c0ad46933aeb4 to your computer and use it in GitHub Desktop.
Save headius/9c28c240ea978617951c0ad46933aeb4 to your computer and use it in GitHub Desktop.
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