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: http://stackoverflow.com/questions/435553/java-reflection-performance  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.

import java.lang.reflect.*;
public class IsReflectionSlow {
public static double rand() {
return Math.random();
}
public static String constant() {
return "constant";
}
public static String concat(String[] strings) {
StringBuilder sb = new StringBuilder();
for (String s : strings)
sb.append(s).append(" ");
return sb.toString();
}
public static void main(String[] args) throws Exception {
testConstant();
testRand();
testConcat(args);
}
private static void testRand() throws Exception {
System.out.println("***** Testing Random number generation *****");
long startStatic = System.currentTimeMillis();
for (int i = 0; i < 100_000_000; i++)
rand();
long stopStatic = System.currentTimeMillis();
System.out.println("Static test results: " + (stopStatic startStatic));
Method m = IsReflectionSlow.class.getMethod("rand", new Class[0]);
Object[] params = new Object[0];
long startReflect = System.currentTimeMillis();
for (int i = 0; i < 100_000_000; i++)
m.invoke(null, params);
long stopReflect = System.currentTimeMillis();
System.out.println("Reflection test results: " + (stopReflect startReflect));
}
private static void testConstant() throws Exception {
System.out.println("***** Testing Constants *****");
long startStatic = System.currentTimeMillis();
for (int i = 0; i < 100_000_000; i++)
constant();
long stopStatic = System.currentTimeMillis();
System.out.println("Static test results: " + (stopStatic startStatic));
Method m = IsReflectionSlow.class.getMethod("constant", new Class[0]);
Object[] params = new Object[0];
long startReflect = System.currentTimeMillis();
for (int i = 0; i < 100_000_000; i++)
m.invoke(null, params);
long stopReflect = System.currentTimeMillis();
System.out.println("Reflection test results: " + (stopReflect startReflect));
}
private static void testConcat(String[] list) throws Exception {
System.out.println("***** Testing Concats *****");
long startStatic = System.currentTimeMillis();
for (int i = 0; i < 10_000_000; i++)
concat(list);
long stopStatic = System.currentTimeMillis();
System.out.println("Static test results: " + (stopStatic startStatic));
Method m = IsReflectionSlow.class.getMethod("concat", new Class[] {String[].class});
long startReflect = System.currentTimeMillis();
for (int i = 0; i < 10_000_000; i++)
m.invoke(null, (Object) list);
long stopReflect = System.currentTimeMillis();
System.out.println("Reflection test results: " + (stopReflect startReflect));
}
}

java IsReflectionSlow a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
***** Testing Constants *****
Static test results: 2
Reflection test results: 212
***** Testing Random number generation *****
Static test results: 2234
Reflection test results: 2697
***** Testing Concats *****
Static test results: 16341
Reflection test results: 16870

view raw
results.txt
hosted with ❤ by GitHub

From → Java

Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: