try {
module.getClass().getMethod(methodName, Serializable.class).invoke(module, message);
} catch (Exception e) {
throw e;
}
Theoretically I knew that reflection works slower than direct invocation. But how slower?
Due to this code was found in the very performance critical part of the system I decided first to perform some benchmarking. I wrote class that contains one method foo() that does nothing and implemented 3 scenarios of invocation and ran them 1 million times:
- direct invocation
- invocation using reflection when getMethod() was called once
- invocation using reflection when getMethod() was called on each loop iteration.
- direct invocation took 5 ms
- reflection took 38 ms
- getMethod() + reflection invocation too 435 ms
Nice overview
ReplyDelete