Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1051370
  • 博文数量: 155
  • 博客积分: 5339
  • 博客等级: 大校
  • 技术积分: 1436
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-10 21:41
文章分类

全部博文(155)

文章存档

2016年(3)

2015年(7)

2014年(3)

2013年(1)

2012年(8)

2011年(5)

2010年(1)

2009年(5)

2008年(4)

2007年(26)

2006年(46)

2005年(46)

分类: Java

2015-01-23 09:56:05

我们所有人都知道java反射的性能要比它已经编译过的静态方法慢,下面是一个小基准测试代码:

点击(此处)折叠或打开

  1. public class TheCostOfReflection {
  2.  
  3.     public static void main(String[] args) throws Exception {
  4.         int nObjects = 100;
  5.         Object[] objects = new Object[nObjects];
  6.  
  7.         // warm up a bit
  8.         int warmupLoops = Math.min(nObjects, 100);
  9.         for (int i = 0; i < warmupLoops; i++) {
  10.             testNewOperator(nObjects, objects);
  11.             testNewInstance(nObjects, objects);
  12.         }
  13.  
  14.         System.gc();
  15.  
  16.         // using new
  17.         System.out.println("Testing 'new'...");
  18.         long newTime = testNewOperator(nObjects, objects);
  19.         System.out.println("Took " + (newTime / 1000000f) + "ms to create " +
  20.                            nObjects + " objects via 'new' (" +
  21.                            (nObjects / (newTime / 1000000f)) +
  22.                            " objects per ms)");
  23.         System.gc();
  24.         // using newInstance() on class
  25.         System.out.println("Testing 'newInstance()'...");
  26.         long niTime = testNewInstance(nObjects, objects);
  27.         System.out.println("Took " + (niTime / 1000000f) + "ms to create " +
  28.                            nObjects + " objects via reflections (" +
  29.                            (nObjects / (niTime / 1000000f)) +
  30.                            " objects per ms)");
  31.         // ratio
  32.         System.out.println("'new' is " + (niTime / (float) newTime) +
  33.                            " times faster than 'newInstance()'.");
  34.     }
  35.  
  36.     private static long testNewInstance(int nObjects, Object[] objects)
  37.             throws Exception {
  38.         long start = System.nanoTime();
  39.         for (int i = 0; i < nObjects; i++) {
  40.             objects[i] = Object.class.newInstance();
  41.         }
  42.         return System.nanoTime() - start;
  43.     }
  44.  
  45.     private static long testNewOperator(int nObjects, Object[] objects) {
  46.         long start = System.nanoTime();
  47.         for (int i = 0; i < nObjects; i++) {
  48.             objects[i] = new Object();
  49.         }
  50.         return System.nanoTime() - start;
  51.     }
  52. }


运行时添加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()'.


阅读(1022) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~