通过反射调用构造函数的

通过反射调用构造函数的

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

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>);

比使用关键字new好吗?

假设该类将被缓存。

最佳答案

以下是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);
}

结果:
$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〜执行第二种方法的速度的2倍

但是,请不要忘记D. Knuth的名言:过早的优化是万恶之源

09-12 15:53