惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
B
Blog RSS Feed
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
MongoDB | Blog
MongoDB | Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
D
Docker
Jina AI
Jina AI
IT之家
IT之家
人人都是产品经理
人人都是产品经理
L
LangChain Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
博客园 - 叶小钗
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog

博客园 - 8user

partial关键字的含义和使用 JS仿Open()打开一个新窗口,并不是弹出新窗口 判断是否含有汉字 - 8user - 博客园 如何统计代码行执行的时间? - 8user - 博客园 时间的正则表达式验证 深入探讨IsPostBack【转】 IIS无法启动的解决方法 FCKEditor的使用及配置方法 Cool MVC: 使用正则式限定路由规则Rount - 8user C#中的值类型和引用类型 C#在线备份数据库至制定目录 - 8user - 博客园 c#实现天气预报查询 C#中的枚举器 C#里面比较时间大小三种方法 - 8user - 博客园 c#中删除ArrayList中的重复数据 - 8user - 博客园 SQL Server数据库开发的二十一条规则 c#实现程序的开机启动 - 8user - 博客园 VS2008 快捷键大全 C#调用dos命令
反射性能测试
8user · 2009-06-10 · via 博客园 - 8user

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