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

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
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 快捷键大全 - 8user 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