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

本文通过实验对比了Java中不同属性设置方法的性能,包括直接赋值、使用CGLIB代理、BeanMap及反射的方式。结果显示,直接赋值最快,而反射最慢。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


测试代码:

ExpandedBlockStart.gif代码
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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值