Skip to content

Instantly share code, notes, and snippets.

@hugo4715
Created July 5, 2020 18:22
Show Gist options
  • Save hugo4715/e44cc7db500285f09407d34993d4dd55 to your computer and use it in GitHub Desktop.
Save hugo4715/e44cc7db500285f09407d34993d4dd55 to your computer and use it in GitHub Desktop.
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class Scratch {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
TestClass testClass = new TestClass();
long time = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
testClass.getClass().getMethod("test").invoke(testClass);
}
System.out.println(System.currentTimeMillis()-time + "ms");
time = System.currentTimeMillis();
Method method = testClass.getClass().getMethod("test");
for (int i = 0; i < 10000000; i++) {
method.invoke(testClass);
}
System.out.println(System.currentTimeMillis() - time + "ms");
}
static class TestClass{
private int a;
public void test(){
a++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment