Java 反射与cglib.proxy与cglib.beanmap与直接赋值 性能对比

出处:http://www.cnblogs.com/zc22/archive/2010/06/20/1761332.html

测试代码:

复制代码
代码
import  java.lang.reflect.Method;
import  java.util.Calendar;
import  java.util.Date;

import  net.sf.cglib.beans.BeanMap;
import  net.sf.cglib.proxy.Enhancer;
import  net.sf.cglib.proxy.MethodInterceptor;
import  net.sf.cglib.proxy.MethodProxy;

public   class  TestPerformance
{
    
public   static   void  main(String[] args)
    {
        
int  times  =   10000000 ;
        
//  TestBean(times); // =15
        
//  TestCglib(times); // =516
        
//  TestBeanMap(times); // =256
        TestReflection(times); //  =11359
    }

    
public   static   void  TestBean( int  times)
    {
        MyBean bean 
=   new  MyBean();

        Date start 
=  Calendar.getInstance().getTime();
        
for  ( int  i  =   0 ; i  <  times; i ++ )
        {
            bean.setName(
" helloworld " );
            Object v 
=  bean.getName();
        }

        Date end 
=  Calendar.getInstance().getTime();

        System.out.println(end.getTime() 
-  start.getTime());
    }

    
public   static   void  TestCglib( int  times)
    {
        Enhancer enhancer 
=   new  Enhancer();
        enhancer.setSuperclass(MyBean.
class );
        enhancer.setCallback(
new  TestMethodInterceptorImpl());
        MyBean my 
=  (MyBean) enhancer.create();

        Date start 
=  Calendar.getInstance().getTime();
        
for  ( int  i  =   0 ; i  <  times; i ++ )
        {
            my.setName(
" helloworld " );
            Object v 
=  my.getName();
        }

        Date end 
=  Calendar.getInstance().getTime();

        System.out.println(end.getTime() 
-  start.getTime());
    }

    
public   static   void  TestBeanMap( int  times)
    {
        MyBean bean 
=   new  MyBean();
        BeanMap map 
=  BeanMap.create(bean);

        Date start 
=  Calendar.getInstance().getTime();
        
for  ( int  i  =   0 ; i  <  times; i ++ )
        {
            map.put(bean, 
" name " " helloworld " );
            Object v 
=  bean.getName();
        }
        Date end 
=  Calendar.getInstance().getTime();

        System.out.println(end.getTime() 
-  start.getTime());
    }

    
public   static   void  TestReflection( int  times)
    {
        MyBean bean 
=   new  MyBean();
        Class c 
=  MyBean. class ;
        
try
        {
            Method get 
=  c.getDeclaredMethod( " getName " null );
            Method set 
=  c.getDeclaredMethod( " setName " , String. class );
            Date start 
=  Calendar.getInstance().getTime();
            
for  ( int  i  =   0 ; i  <  times; i ++ )
            {
                set.invoke(bean, 
" helloworld " );
                Object v 
=  get.invoke(bean,  null );
            }
            Date end 
=  Calendar.getInstance().getTime();

            System.out.println(end.getTime() 
-  start.getTime());
        } 
catch  (Exception ex)
        {

        }
    }
}

class  TestMethodInterceptorImpl  implements  MethodInterceptor
{
    
public  Object intercept(Object obj, Method method, Object[] args,
            MethodProxy proxy) 
throws  Throwable
    {
        
return  proxy.invokeSuper(obj, args);
    }
}

class  MyBean
{
    
private  String name;

    
public  String getName()
    {
        
return  name;
    }

    
public   void  setName(String name)
    {
        
this .name  =  name;
    }
}
复制代码

 

测试结果:

直接赋值 = 15

cglib.proxy = 516

cglib.beanmap = 256

reflection = 11359


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值