FastMethod ist ein Werkzeug zum schnellen, dynamischen Aufrufen von Methoden. FastMethod ist aus dem Intersult Maven Repository zu beziehen.
public static final long N = 10000000000L;
public void testFastMethod() throws Exception {
FastMethod method = FastMethod.create(FastMethodTest.class, "someMethod");
method.invoke(this);
long begin = System.nanoTime();
for (long i = 0; i < N; ++i) {
method.invoke(this);
}
long duration = System.nanoTime() - begin;
System.out.println("FastMethod: " + duration / 1000000000D + " s");
}
public void testMethod() throws Exception {
Method method = FastMethodTest.class.getDeclaredMethod("someMethod");
method.invoke(this);
long begin = System.nanoTime();
for (long i = 0; i < 10000000000L; ++i) {
method.invoke(this);
}
long duration = System.nanoTime() - begin;
System.out.println("Method: " + duration / 1000000000D + " s");
}
Ergebnis ist eine Beschleunigung um etwa den Faktor 16:
Method: 100.797758873 s FastMethod: 6.42633348 s