通过反射调用构造函数的性能

hnn*_*nnn 4 java reflection performance

通过反射调用构造函数的速度有多少,例如:

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>);
Run Code Online (Sandbox Code Playgroud)

比使用关键字new

假设该类将被缓存.

Jir*_*ser 5

以下是JMH针对以下方法测量的结果:

@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);
}
Run Code Online (Sandbox Code Playgroud)

结果:

$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
Run Code Online (Sandbox Code Playgroud)

2690194/1332644~ 执行第二种方法的速度快2倍

但是不要忘记D. Knuth的话:过早的优化是万恶之源

  • @RafaelWinterhalter我为缓存的构造函数添加了一个基准,结果几乎相同 (3认同)
  • 遗憾的是,这种JMH测试无效.请参阅此示例以了解原因:http://hg.openjdk.java.net/code-tools/jmh/file/7098caa2753f/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_08_DeadCode.java (2认同)