反射性能测试


源代码:TestReflection.rar
1、新建一个空的解决方案。
2、向解决方案中添加三个类库项目,分别为IDAL,DAL,BLL。
3、新建一个用于测试的WEB项目。

4、向IDAL中添加IReflection接口:
using System;
using System.Collections.Generic;
using System.Text;

namespace IDAL
{
    public interface IReflection
    {
        void Test();
    }
}

5、向DAL中添加ReflectionDA类:
using System;
using System.Collections.Generic;
using System.Text;
using IDAL;

namespace DAL
{
    public class ReflectionDA:IReflection
    {
        public void Test()
        {
            return;
        }
    }
}

6、向BLL添加测试类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using IDAL;

namespace BLL
{
    /// <summary>
    /// 测试反射的效率
    /// </summary>
    /// <remarks>
    /// 作者:☆聊ゾ聊☆
    /// 时间:2007-03-26
    /// </remarks>
    public class ReflectionBL
    {
        /// <summary>
        /// 每次反射创建一个实例,然后利用接口调用类的方法
        /// </summary>
        /// <param name="count">方法调用次数</param>
        /// <returns>调用方法所花费的时间</returns>
        public TimeSpan TestFromInterface(int count)
        {
            //反射调用
            DateTime startR = DateTime.Now;
            for (int i = 0; i < count; i++)
            {
                Assembly assembly = Assembly.LoadFile(GetDLLPath());
                Type type = assembly.GetType("DAL.ReflectionDA");
                ConstructorInfo conInfo = type.GetConstructor(new Type[] { });
                object obj = conInfo.Invoke(new object[] { });

                IReflection reflection = (IReflection)obj;
                reflection.Test();
            }

            return DateTime.Now - startR;
        }

        /// <summary>
        /// 每次反射创建一个实例,然后反射调用类的方法
        /// </summary>
        /// <param name="count">方法调用次数</param>
        /// <returns>调用方法所花费的时间</returns>
        public TimeSpan TestFromReflection(int count)
        {
            //基于接口的反射调用
            DateTime startR = DateTime.Now;
            for (int i = 0; i < count; i++)
            {
                Assembly assembly = Assembly.LoadFile(GetDLLPath());
                Type type = assembly.GetType("DAL.ReflectionDA");
                ConstructorInfo conInfo = type.GetConstructor(new Type[] { });
                object obj = conInfo.Invoke(new object[] { });

                object rtn;
                rtn = type.GetMethod("Test").Invoke(obj, new object[] { });
            }

            return DateTime.Now - startR;
        }

        /// <summary>
        /// 每次用new 关键字实例化一个类,并调用其方法
        /// </summary>
        /// <param name="count">方法调用次数</param>
        /// <returns>调用方法所花费的时间</returns>
        public TimeSpan Test(int count)
        {
            DateTime startR = DateTime.Now;
            for (int i = 0; i < count; i++)
            {
                DAL.ReflectionDA dal = new DAL.ReflectionDA();
                dal.Test();
            }
            return DateTime.Now - startR;
        }

        /// <summary>
        /// 获取动态链接库所在的路径
        /// </summary>
        /// <returns>动态链接库所在的路径</returns>
        private string GetDLLPath()
        {
            return @"F:\localhost\TestReflection\DAL\bin\Debug\DAL.dll";
        }

        /// <summary>
        /// 先用反射创建对象,然后再利用接口调用方法
        /// </summary>
        /// <param name="count">调用方法所花费的时间</param>
        /// <returns>调用方法所花费的时间</returns>
        public TimeSpan TestFromInterface1(int count)
        {

            Assembly assembly = Assembly.LoadFile(GetDLLPath());
            Type type = assembly.GetType("DAL.ReflectionDA");
            ConstructorInfo conInfo = type.GetConstructor(new Type[] { });
            object obj = conInfo.Invoke(new object[] { });

            //反射调用
            DateTime startR = DateTime.Now;
            for (int i = 0; i < count; i++)
            {
                IReflection reflection = (IReflection)obj;
                reflection.Test();
            }

            return DateTime.Now - startR;
        }

        /// <summary>
        /// 先创建对象的实例,再反射调用方法
        /// </summary>
        /// <param name="count">调用方法所花费的时间</param>
        /// <returns>调用方法所花费的时间</returns>
        public TimeSpan TestFromReflection1(int count)
        {
            //基于接口的反射调用

            Assembly assembly = Assembly.LoadFile(GetDLLPath());
            Type type = assembly.GetType("DAL.ReflectionDA");
            ConstructorInfo conInfo = type.GetConstructor(new Type[] { });
            object obj = conInfo.Invoke(new object[] { });

            DateTime startR = DateTime.Now;
            for (int i = 0; i < count; i++)
            {
                object rtn;
                rtn = type.GetMethod("Test").Invoke(obj, new object[] { });
            }

            return DateTime.Now - startR;
        }
    }
}

在我本地机器上的测试结果如下:

频繁创建对象实例:
反射方法:00:00:01.9843750
接口调用:00:00:01.9687500

提前创建对象实例:
反射方法:00:00:00.3750000
接口调用:00:00:00
直接调用:00:00:00

机器配置:AMD 3000+ 64位/1G内存/MS WIN2K3

转载于:https://www.cnblogs.com/hblynn/archive/2007/03/26/688815.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值