Java 反射與cglib.proxy與cglib.beanmap與直接賦值 效能對比

來源:互聯網
上載者:User

測試代碼:

代碼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

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.