您的位置:首页 > 编程语言 > Java开发

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

2014-08-28 15:40 736 查看
出处: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java性能优化