Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Is reflection in Java a performance killer? Run these tests to check for yourself. After compilation, just run with the command line below passing a list of any strings as command line input to args[].
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.