Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of invoking constructor by reflection

How many times is it slower to invoke constructor by reflection eg.:

Class c = map.get("somestr");
Constructor t = c.getDeclaredConstructor(int.class,MyClass.class);
MyObject o = (MyObject)t.invoke(<params>);
or
Constructor t = map.get("somestr");
MyObject o = (MyObject)t.invoke(<params>);

than using the keyword new?

Assuming the class will be cached.

like image 550
hnnn Avatar asked Jan 19 '15 17:01

hnnn


1 Answers

Here are results measured by JMH for following methods:

@Benchmark
@BenchmarkMode(Mode.Throughput)
public List<Integer> test1() throws Exception {
    Constructor<ArrayList> constructor = ArrayList.class.getDeclaredConstructor(int.class);
    return constructor.newInstance(42);
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
public List<Integer> test2() throws Exception {
    return new ArrayList(42);
}

private Constructor<ArrayList> constructor = ArrayList.class.getDeclaredConstructor(int.class);

@Benchmark
@BenchmarkMode(Mode.Throughput)
public List<Integer> test3() throws Exception {
    return constructor.newInstance(42);
}

results:

$java -jar benchmarks.jar -wi 5 -bs 5 -i5 -f3
...
# Warmup: 5 iterations, 1 s each
# Measurement: 5 iterations, 1 s each, 5 calls per op
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
...
test1:
Result: 1332644.380 ±(99.9%) 325213.907 ops/s [Average]
  Statistics: (min, avg, max) = (327336.040, 1332644.380, 1532709.773), stdev = 304205.290
  Confidence interval (99.9%): [1007430.473, 1657858.287]
...
test2:
Result: 2690194.908 ±(99.9%) 260758.633 ops/s [Average]
  Statistics: (min, avg, max) = (2173201.836, 2690194.908, 3047133.124), stdev = 243913.787
  Confidence interval (99.9%): [2429436.275, 2950953.540]
...
test3:
Result: 2555898.218 ±(99.9%) 194215.211 ops/s [Average]
  Statistics: (min, avg, max) = (2046939.228, 2555898.218, 2707224.497), stdev = 181669.029
  Confidence interval (99.9%): [2361683.007, 2750113.429]


# Run complete. Total time: 00:01:07

Benchmark Mode   Samples        Score        Error  Units
test1     thrpt       15  1332644.380 ± 325213.907  ops/s
test2     thrpt       15  2690194.908 ± 260758.633  ops/s
test3     thrpt       15  2555898.218 ± 194215.211  ops/s

2690194/1332644 ~ it was 2 times faster to execute the second method

But don't forget the D. Knuth's quote: Premature optimization is the root of all evil

like image 90
Jiri Kremser Avatar answered Sep 28 '22 08:09

Jiri Kremser