(为什么)是反映在净这么贵? [英] (Why) Is Reflection so expensive in .Net?

查看:150
本文介绍了(为什么)是反映在净这么贵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  什么是“成本”的反射?

有没有人有普遍接受的口头禅反射==糟糕表现的一个很好的解释?

Does anyone have a good explanation of the generally accepted mantra that reflection == bad performance?

例如,是多么昂贵它从这种类型的实例遍历一个类型的属性集合,并提取所有属性值比例仅为直接访问所有属性?一个数量级?二?这是什么依赖?它是predictable呢?这是怎么回事引擎盖下?

For example, how expensive is it to iterate over a type's properties collection and extract all property values from an instance of this type compared to just accessing all the properties directly? One level of magnitude? Two? What does it depend on? Is it predictable at all? What is happening under the hood?

编辑:谢谢你的答案为止。我已经研究过一些你提供的链接,它似乎没有在那里就思考属性相比,直接访问估计一个巨大差异:从2.5倍慢200倍慢。

Thanks for the answers so far. I've looked into some the links you provided and it seems that there is a vast gap of estimates out there regarding Reflection on Properties compared to direct access: from 2.5 times slower to 200 times slower.

这似乎不是很合理的,我。你们有些人提到更高版本的.Net的性能改进,以便让被缩小我的问题到.NET 4.0。没有人有任何的基准是什么?

This does not seem very reasonable to me. Some of you mentioned performance improvements in later versions of .Net so let be narrow my question to .Net 4.0. Does anyone have any benchmarks for that?

推荐答案

最好的答案是,普遍接受的口头禅是不是像看起来那么简单。 反射==糟糕表现在很大程度上起源于.NET 1.0和1.1,并没有认识到更高版本的性能改进。

The best answer is that the generally accepted mantra is not as simple as it seems. reflection == bad performance largely originated with .NET 1.0 and 1.1, and fails to acknowledge the performance improvements in later versions.

要客观,我测试了基于反射的解决方案与非反射基础的解决方案在多个场合 - 而获胜者并不总是一方或另一方。反思是它是什么,它​​的工作原理是如何工作的,它不是consistenly更快或更慢,它(与基本上所有的编程方法)不能被视为银弹或东西始终是可以避免的。

To be objective I've tested reflection-based solutions versus non-reflection based solutions on a number of occasions--and the winner isn't always one or the other. Reflection is what it is and it works how it works, it's not consistenly faster or slower and it (as with basically all programming approaches) can't be treated as a silver-bullet or as something to always be avoided.

这篇关于(为什么)是反映在净这么贵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆