Skip to content

Is Java Reflection Slow?

July 15, 2013

You still hear people say “Java reflection is so slow” or “You just killed your performance by using reflection”, but I’ve yet to hear any hard numbers. Some people claim it’s an order of magnitude more expensive:  How about we run some tests to get the real story.

Below I’ve written some example code to micro-benchmark a comparison of typical Java method invocation vs reflection based method invocation. The results are exactly what I’d expect from the JVM. Any optimizable function (like returning a constant, or a method that only depends on inputs, or simple operations like swapping integers in memory) experiences a noticeable slowdown due to the work being performed in the method being so fast. But when the method has to perform any sort of I/O or complex task like string concatenation or random number generation, the difference becomes extremely small.

So the results below demonstrate that for all but the most trivial of operations, reflection is a tiny and sometimes non-existent overhead. Non-existent sometimes because the variability in the performance of the internals of the method itself might wipe away the cost of reflection. Reflection is NOT SLOW.

From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: