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

推荐订阅源

I
Intezer
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
N
News | PayPal Newsroom
T
Tenable Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
P
Privacy International News Feed
IT之家
IT之家
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
博客园_首页
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
I
InfoQ
D
DataBreaches.Net
有赞技术团队
有赞技术团队
K
Kaspersky official blog
Security Latest
Security Latest
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic

博客园 - 廖勇军

关于c++的头文件依赖 增强资源管理器右键功能,含源代码 VC中结构体的内存布局 进程间共享句柄三种方式 SocanCode连接Oracle的方法 SocanCode7之模板编写 javascript总结 IIS7.0中使用MVC3,静态页正常,其它404 ashx的使用 一起来灭掉IE6! iis express感觉还不错 关于sqlite使用entity framework的布署问题 - 廖勇军 - 博客园 负margin实现div的左右排版 原来Jquery.load的方法可以一直load下去 错误1067进程意外终止 关于省市联动的问题想法 javac编译多个带package文件 远程服务器返回了错误 NOTFOUND Java程序放到Linux上出现的问题
不用再纠结反射影响效率了
廖勇军 · 2012-08-03 · via 博客园 - 廖勇军

对于网上流传的“反射效率低”的说法,本人一直是相信的,这是动态和静态的区别,但反射到底影响多大程序一直没测试过,今天本着求是的态度做了个测试

using System;
using System.Diagnostics;
using System.Reflection;

class Program4
{
    static void Main()
    {
        Assembly assembly = Assembly.GetExecutingAssembly();
        Module[] modules = assembly.GetModules(false);
        Type type = assembly.GetType("ThreadClass");
        MethodInfo mi = type.GetMethod("test");

        Stopwatch watch = new Stopwatch();
        watch.Start();
        for (int i = 0; i < 10000; i++)
        {
            ThreadClass.test();
        }
        watch.Stop();
        Console.WriteLine(watch.ElapsedTicks);

        watch.Restart();
        for (int i = 0; i < 10000; i++)
        {
            mi.Invoke(null, null);
        }
        watch.Stop();
        Console.WriteLine(watch.ElapsedTicks);

        Console.Read();
    }
}

class ThreadClass
{
    public static void test()
    {
        //nothing
    }
}

要调用的方法里什么都不做,这样能更好地显示出“纯粹调用”的差距,结果打印:

772
9345

从结果上来看,不到10000个ticks(10000个ticks相当于1毫秒),这说明纯粹的调用差别,一万次以上才会有一毫秒的误差,够小了吧,假设调用的方法里再做点事,这点差别根本忽略不计。

这里要注意的是:调用之前已经获取到了MethodInfo,也许反射最大的性能问题应该在“加载程序集、获取类、获取方法信息”上,因此程序中只要在第一次调用时获取到MethodInfo,以后直接使用,根本没有什么效率的问题可以担心的,除非你傻到每次调用时都做“加载程序集、获取类、获取方法信息”这一系列动作。

总结:只要提前获取到MethodInfo(确切的说是第一次,之后直接使用),10000次以上的调用才有1毫秒的误差,根本不存在效率的担心,放心地使用反射吧!