论坛首页 Java企业应用论坛

我做了个简单的性能测试,发现从bean工厂里单例执行方法效率比new对象执行慢很多

浏览 2576 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (11) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-03-16  

同时还发现调用static方法的性能并没比new Object的性能好——不解ing

测试场景:

Punit 100个线程,测试调用的方法无方法体。

 

环境是

Jdk1.6

spring2.X

 

代码都在附件里——大家发表下意见啊

   发表时间:2012-03-16  
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
[concurrent] Starting test4.TestMethodInvoke
test4.TestMethodInvoke
testStatic() - [0.033734ms]
testStaticDirect() - [0.005866ms]
total: 2, failures:0 (GREEN) - 50.155117ms
[concurrent] Starting test4.TestMethodInvoke2
test4.TestMethodInvoke2
testNotStatic() - [0.013689ms]
testNotStaticDirect() - [0.005378ms]
total: 2, failures:0 (GREEN) - 28.44307ms
[concurrent] Starting test4.TestMethodInvoke3
test4.TestMethodInvoke3
testSingleton() - [26.431292ms]
testSingletonDirect() - [0.027867ms]
total: 2, failures:0 (GREEN) - 53.250762ms
0 请登录后投票
   发表时间:2012-03-20   最后修改:2012-03-20
参考一下这个例子吧。
/**
	 * 反射的效率
	 */
	public void test5() {
		User user = new User("test", 1);
		long times = 1000000L;
		long sDt = System.currentTimeMillis();
		for (long i = 0; i < times; i++) {
			user.getName();
		}
		long eDt = System.currentTimeMillis();
		System.out.println("直接调用花费时间:" + (eDt - sDt) + "ms");

		try {
			sDt = System.currentTimeMillis();
			Method m = User.class.getDeclaredMethod("getName", null);
			sDt = System.currentTimeMillis();
			for (long i = 0; i < times; i++) {
				m.invoke(user, null);
			}
			eDt = System.currentTimeMillis();
			System.out.println("反射调用花费时间:" + (eDt - sDt) + "ms");
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

		try {
			sDt = System.currentTimeMillis();
			for (long i = 0; i < times; i++) {
				PropertyUtils.getProperty(user, "name");
			}
			eDt = System.currentTimeMillis();
			System.out.println("BeanUtils调用花费时间:" + (eDt - sDt) + "ms");
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
0 请登录后投票
   发表时间:2012-03-20   最后修改:2012-03-20
因为我所在的一些应用都是只拿spring工厂做bean配置的——就是获取bean而已,没其他用。

这么做和直接new一个对象有什么区别,向lift框架还是play框架有都是static的方法,这些也和new一个对象有什么区别

现在我越来越感觉一些框架搞来搞去,包括语言的发展所带来的特性,在一些应用场景下都只是特性而已,不实用(如注解对于DI,web url route和参数绑定等)

0 请登录后投票
   发表时间:2012-03-21  
spring的轻量和扩展特性,值得我们牺牲一点点运行效率。
0 请登录后投票
   发表时间:2012-03-21  
当然有用——就是滥用太多——

不仅是效率问题,这样搞本来静态方法提供类的也放到beanfactory,new一个对象的也放到beanfactory,大家都对这种所有bean都应该运行到容器里膜拜了
0 请登录后投票
   发表时间:2012-03-21  
单例应该比较省内存吧,另外在对象比较轻的情况下,问题不大,但是如果是很重的对象,还是多例比较好。另外还要看对象内的调用是否会有资源竞争的情况。个人理解。
0 请登录后投票
   发表时间:2012-03-21  
我们应该看到把对象的生成交给spring去管理,对于类本身的特点(单例),生命周期管理(lazy-init),代理模式等等,这么多的好处理应让我们去用spring.
刚刚做一个测试,用XmlBeanFactory去生成一个对象,并没比new一个对象慢很多,大概200ms左右,而且也有缓存。
对于静态方法,当然不推荐用spring去管理。静态方法本身存储于JVM静态存储区中,直接调用更加快。
单例,多例,我觉得应该看一下对象的业务特性,是有状态的,还是没状态的。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics