cglib 的 beanMap 与 反射 的效率

一直在用cglib,突然想起来还没有测试过他的效率到底如何,所以稍微测试了一下
对于有11个成员变量的bean,一个是直接使用java的反射读取,另一个是使用cglib转换beanMap以后直接使用get读取。(详细结果见下表)
可以看出, cglib 的效率是 直接反射的3倍(字节码编程确实很厉害!)另外,cglib的beanMap在初次创建一个类的缓存时会耗费一定的时间(300ms),之后使用了缓存,再创建相同的bean就不再有多余的时间消耗了。

 

 

cglib反射cglib反射cglib反射
循环10000次1000001000000
时间(毫秒)3441567191234420311531
时间(毫秒)471254381218385911391
时间(毫秒)311254071406378111500
时间(毫秒)471254371250379711484
时间(毫秒)471254071218378111547
时间(毫秒)311254381219378111469
时间(毫秒)311254211157443811453
时间(毫秒)311103901172379711406
时间(毫秒)471254061172379711484
时间(毫秒)311253911156389111609
时间(毫秒)471253911172417211328
时间(毫秒)311254061234381311343
时间(毫秒)311254071203381311437
时间(毫秒)322184221203378211797
时间(毫秒)782354381500395311906
时间(毫秒)621254061922409411515
时间(毫秒)471254691375384411531
时间(毫秒)311254371266379711766
时间(毫秒)471104371266378111594
时间(毫秒)471094061344378111453
总平均:57134.44341284389811527.2
两者的速度比:0.4241071430.337641610.33813502
除第一次平均:41.894193882
初次多用时:302.1300321.3

 

 

public class CglibSpeedTest {
	
	int size = 1000000;
	
	List catchList = null;
	
	public List<SettleSubs> getInitData() {
		if (catchList != null ) {
			return catchList;
		}
		catchList = new ArrayList<SettleSubs>(size);
		for (int i = 0; i < size; i++) {
			SettleSubs settle = new SettleSubs();;
			settle.setBuyorsell   ("s1s");
			settle.setClientId    ("s2s");
			settle.setContractId  ("s3s");
			settle.setDayStr      ("s4s");
			settle.setDetailSeq   (1   );
			settle.setContractType("s5s");
			settle.setCsgWarehouse("s6s");
			settle.setDealPrice   (2  );
			settle.setSettlePrice (3  );
			settle.setTotalQtt    (4   );
			settle.setRcptQtt     (5  );
			settle.setFrzCsgQtt   (6  );
			settle.setDepositAmt  (7   );
			settle.setSubsBalance (8   );
			settle.setConveyQtt   (9   );
			settle.setCnyBalance  (10   );
			catchList.add(settle);
		}
		return catchList;
	}
	
	public long testReflectance() throws Exception {
		List<SettleSubs> list = getInitData();
//		System.out.println("开始" + DateUtil.format(new Date()));
		long start = System.currentTimeMillis();
		Class clazz = SettleSubs.class;
		List<Method> listMethods = new ArrayList<Method>();
		for (Method method : clazz.getMethods()) {
			if (method.getName().startsWith("get")) {
				listMethods.add(method);
			}
		}
		
		for (SettleSubs settle: list) {
//			StringBuilder sb = new StringBuilder();
			for (Method method : listMethods) {
				method.invoke(settle);
			}
//			System.out.println(sb);
		}
//		System.out.println(System.currentTimeMillis() - start);
//		System.out.println("完成:" + DateUtil.format(new Date()));
		return System.currentTimeMillis() - start;
	}
	
	public long testCglib() throws Exception {
		List<SettleSubs> list = getInitData();
//		System.out.println("开始" + DateUtil.format(new Date()));
		long start = System.currentTimeMillis();
		Set keyset = BeanMap.create(list.get(0)).keySet();
		for (SettleSubs settle: list) {
			BeanMap map = BeanMap.create(settle);
			for (Object key : keyset) {
				map.get(key);
			}
			
		}
//		System.out.println(System.currentTimeMillis() - start);
//		System.out.println("完成:" + DateUtil.format(new Date()));
		return System.currentTimeMillis() - start;
	}
	
	public static void main(String[] args) throws Exception {
//		new CglibSpeedTest().testReflectance();
		CglibSpeedTest test = new CglibSpeedTest();
		System.out.println("次数:" + test.size);
		for (int i = 0; i < 20; i++) {
			System.out.println(test.testCglib() + "\t" + test.testReflectance());
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值