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

Java 通过反射调用构造函数的性能,java,performance,reflection,Java,Performance,Reflection,通过反射调用构造函数的速度要慢多少倍,例如: 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

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

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>);
类c=map.get(“somestr”); 构造函数t=c.getDeclaredConstructor(int.class,MyClass.class); MyObject o=(MyObject)t.invoke(); 或 构造函数t=map.get(“somestr”); MyObject o=(MyObject)t.invoke(); 而不是使用关键字
new


假设类将被缓存。

以下是通过以下方法测量的结果:

@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);
}
2690194/1332644~执行第二种方法的速度比执行第二种方法快了两倍


但别忘了D.Knuth的名言:过早优化是万恶之源

你在这里寻找什么样的答案?调查Java中的微基准点,然后进行比较或忘记它。如果你在编译时知道目标类,为什么要处理意大利面以反射方式实例化?另外,对于大多数应用程序来说,每秒创建729462个对象应该足够好了。花费您的是查找,而不是调用。一个更好的缓存策略可以解决这个问题。不幸的是,这个JMH测试是无效的。查看此示例以了解原因:@RafaelWinterhalter我为缓存的构造函数添加了一个基准,结果几乎相同
$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