我们所有人都知道java反射的性能要比它已经编译过的静态方法慢,下面是一个小基准测试代码:
-
public class TheCostOfReflection {
-
-
public static void main(String[] args) throws Exception {
-
int nObjects = 100;
-
Object[] objects = new Object[nObjects];
-
-
// warm up a bit
-
int warmupLoops = Math.min(nObjects, 100);
-
for (int i = 0; i < warmupLoops; i++) {
-
testNewOperator(nObjects, objects);
-
testNewInstance(nObjects, objects);
-
}
-
-
System.gc();
-
-
// using new
-
System.out.println("Testing 'new'...");
-
long newTime = testNewOperator(nObjects, objects);
-
System.out.println("Took " + (newTime / 1000000f) + "ms to create " +
-
nObjects + " objects via 'new' (" +
-
(nObjects / (newTime / 1000000f)) +
-
" objects per ms)");
-
System.gc();
-
// using newInstance() on class
-
System.out.println("Testing 'newInstance()'...");
-
long niTime = testNewInstance(nObjects, objects);
-
System.out.println("Took " + (niTime / 1000000f) + "ms to create " +
-
nObjects + " objects via reflections (" +
-
(nObjects / (niTime / 1000000f)) +
-
" objects per ms)");
-
// ratio
-
System.out.println("'new' is " + (niTime / (float) newTime) +
-
" times faster than 'newInstance()'.");
-
}
-
-
private static long testNewInstance(int nObjects, Object[] objects)
-
throws Exception {
-
long start = System.nanoTime();
-
for (int i = 0; i < nObjects; i++) {
-
objects[i] = Object.class.newInstance();
-
}
-
return System.nanoTime() - start;
-
}
-
-
private static long testNewOperator(int nObjects, Object[] objects) {
-
long start = System.nanoTime();
-
for (int i = 0; i < nObjects; i++) {
-
objects[i] = new Object();
-
}
-
return System.nanoTime() - start;
-
}
-
}
运行时添加vm参数-Xms512m -Xmx512m (足够大的内存避免运行过程中的GC操作),结果:
Testing 'new'...
Took 7.610116ms to create 1000000 objects via 'new' (131404.05 objects per ms)
Testing 'newInstance()'...
Took 184.72641ms to create 1000000 objects via reflections (5413.41 objects per ms)
'new' is 24.273798 times faster than 'newInstance()'.
反射方法慢25倍左右,如果调低nObjects的值,下面是测试结果:
Testing 'new'...
Took 0.002794ms to create 100 objects via 'new' (35790.98 objects per ms)
Testing 'newInstance()'...
Took 0.021581ms to create 100 objects via reflections (4633.7056 objects per ms)
'new' is 7.7240515 times faster than 'newInstance()'.
添加上-server 参数后,奇妙的事情发生了:
Testing 'new'...
Took 8.862299ms to create 1000000 objects via 'new' (112837.54 objects per ms)
Testing 'newInstance()'...
Took 13.91287ms to create 1000000 objects via reflections (71875.88 objects per ms)
'new' is 1.5698942 times faster than 'newInstance()'.
创建100个对象:
Testing 'new'...
Took 0.002864ms to create 100 objects via 'new' (34916.20 objects per ms)
Testing 'newInstance()'...
Took 0.006007ms to create 100 objects via reflections (16647.24 objects per ms)
'new' is 2.0974162 times faster than 'newInstance()'.
当JIT的魔法介入时,我们获得了神奇的性能提升
小提示:即使在增加了预热代码后性能获得了非常大的提升,我还是建议你使用老的工厂模式(Factory pattern)。除非你在代码的非关键区域创建一些实例,工厂模式还是更好的方式,毕竟newInstance()不可能像new一样快。
原文链接:
自测结果:(100个时浮动较大)
Testing 'new'...
Took 0.001621ms to create 100 objects via 'new' (61690.316 objects per ms)
Testing 'newInstance()'...
Took 0.023908ms to create 100 objects via reflections (4182.7 objects per ms)
'new' is 14.74892 times faster than 'newInstance()'.
Testing 'new'...
Took 15.572841ms to create 1000000 objects via 'new' (64214.36 objects per ms)
Testing 'newInstance()'...
Took 220.04909ms to create 1000000 objects via reflections (4544.4404 objects per ms)
'new' is 14.130311 times faster than 'newInstance()'.
添加-server参数后:
Testing 'new'...
Took 10.71147ms to create 1000000 objects via 'new' (93357.87 objects per ms)
Testing 'newInstance()'...
Took 17.555157ms to create 1000000 objects via reflections (56963.32 objects per ms)
'new' is 1.638912 times faster than 'newInstance()'.
Testing 'new'...
Took 11.121142ms to create 1000000 objects via 'new' (89918.82 objects per ms)
Testing 'newInstance()'...
Took 17.478975ms to create 1000000 objects via reflections (57211.594 objects per ms)
'new' is 1.571689 times faster than 'newInstance()'.
100次结果极不稳定:
Testing 'new'...
Took 0.012967ms to create 100 objects via 'new' (7711.8843 objects per ms)
Testing 'newInstance()'...
Took 0.200581ms to create 100 objects via reflections (498.5517 objects per ms)
'new' is 15.468575 times faster than 'newInstance()'.
Testing 'new'...
Took 0.013777ms to create 100 objects via 'new' (7258.474 objects per ms)
Testing 'newInstance()'...
Took 0.141015ms to create 100 objects via reflections (709.1445 objects per ms)
'new' is 10.235538 times faster than 'newInstance()'.
Testing 'new'...
Took 0.00932ms to create 100 objects via 'new' (10729.613 objects per ms)
Testing 'newInstance()'...
Took 0.200177ms to create 100 objects via reflections (499.5579 objects per ms)
'new' is 21.478218 times faster than 'newInstance()'.
Testing 'new'...
Took 0.005268ms to create 100 objects via 'new' (18982.535 objects per ms)
Testing 'newInstance()'...
Took 0.005673ms to create 100 objects via reflections (17627.357 objects per ms)
'new' is 1.0768793 times faster than 'newInstance()'.
1000次结果相对稳定:
Testing 'new'...
Took 0.012967ms to create 1000 objects via 'new' (77118.84 objects per ms)
Testing 'newInstance()'...
Took 0.024313ms to create 1000 objects via reflections (41130.26 objects per ms)
'new' is 1.8749903 times faster than 'newInstance()'.
Testing 'new'...
Took 0.011346ms to create 1000 objects via 'new' (88136.79 objects per ms)
Testing 'newInstance()'...
Took 0.024313ms to create 1000 objects via reflections (41130.26 objects per ms)
'new' is 2.1428697 times faster than 'newInstance()'.
阅读(1026) | 评论(0) | 转发(0) |