反思是很慢? [英] Is Reflection really slow?

查看:118
本文介绍了反思是很慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个共同的信念,反思是缓慢的,尽量避免它尽可能。但是,这是真正的信仰,在目前的情况如何?已经有很多的变化在当前版本的.NET一样,使用IL织造(即IL的Emit)等,而不是进行反思传统的PropertyInfo和MethodInfo的方式。

This is a common belief that reflection is slow and try to avoid it as much as possible. But is that belief true, in the current situation? There has been lot of changes in the current .net versions like, use of IL Weaving (i.e. IL Emit) etc, as opposed to traditional PropertyInfo and MethodInfo ways of performing reflection.

时,任何令人信服的证据,即在新的反射是不减缓任何更多的,并且可以使用。有没有更好的方式来读取属性数据?

Is that any convincing proof, that the new reflection is not that slow any more, and can be used. Is there better way to read attribute data?

谢谢,
巴斯卡尔

Thanks, Bhaskar

推荐答案

当你想想看,反思是相当的是如何快速织补令人印象深刻。

When you think about it, reflection is pretty darn impressive in how fast it is.

从一个缓存的代表 ConstructorInfo 的MethodInfo 可与速度堪比任何其他代表被调用。

A cached delegate from a ConstructorInfo or MethodInfo can be called with speed comparable to any other delegate.

一个委托从 ILGenerator.Emit (这亦是不是新的,它已经在.NET自第1版)同样可以一样快叫。

A delegate created from ILGenerator.Emit (which incidentally isn't new, it's been in .NET since version 1) can likewise be called just as fast.

通过发光或调用 ConstructorInfo 的代表将是一样快,任何其他对象获取的对象。

An object obtained through emitting or calling a ConstructorInfo's delegate will be just as fast as any other object.

如果您通过动态加载的程序集,使用反射找到调用该方法,并调用它获得一个对象,它实现了一个定义的接口,通过它您从该点调用它,那么它。'会是一样快,在如何使用它作为界面的另一个实施

If you obtain an object by loading an assembly dynamically, using reflection to find the method to call, and calling it, and it implements a defined interface through which you call it from that point on, then it'll be just as fast in how it's used as another implementation of that interface.

在所有的,反映给我们做的事情,没有它,我们会的方式 - 如果我们可以做他们在所有 - 必须使用速度较慢既代码并执行技术

In all, reflection gives us ways of doing things that without it we would - if we could do them at all - have to use techniques that are slower both to code and to execute.

这也给了我们意味着做的事情比较复杂,比较脆的少类型安全的,高性能少比其他手段了。相当多的C#代码的每一行可通过使用反射的代码,一大块被替换。这段代码几乎肯定会比一大堆的方式原线差,和性能是最他们。

It also gives us means of doing things that are more complicated, more brittle, less type-safe and less performant than other means too. Pretty much every line of C# code can be replaced by a big chunk of code that uses reflection. This code will almost certainly be worse than the original line in a whole bunch of ways, and performance is the least of them.

很可能是避免反光,因为它的速度慢这将是慢比这将是建议从信仰的那种开发商将去坚果任何新的技术,只是因为它看起来很酷将是会被更容易警告过的那种源于地道的少,更容易出错,难以维持。很可能这种信念是完全正确的。

Quite possibly the "avoid reflection because its slow" advice stems from the belief that the sort of developer that would go nuts for any new technique just because it seemed cool would be the sort that would be more likely warned off by "it'll be slower" than by "it'll be less idiomatic, more error-prone and harder to maintain". Quite possibly this belief is completely correct.

虽然在大多数情况下,当最自然和明显的方法是使用反射,那么它也不会少高性能。不是一个真正令人费解的企图,以避免它

For the most part though, when the most natural and obvious approach is to use reflection, then it also won't be less performant than a really convoluted attempt to avoid it.

如果性能问题适用于任何在反思,它真的是隐藏的用途:

If performance concerns apply to anything in reflection, its really to the uses that are hidden:

使用动态可以的情况下似乎合理的,其中只有一点点的工作能够避免它。这里的性能差异可能是值得考虑的。

Using dynamic can seem sensible in a case where only a little work could avoid it. Here the performance difference may be worth considering.

在ASP.NET中,使用<%#DataBinder.Eval的(的Container.DataItem,SomeProperty )%> <更方便,但一般不太高性能;#((SOMETYPE)的Container.DataItem).SomeProperty%GT; <%#SomeCodeBehindProvidedCallWithTheSameResult%GT; 。我还是会使用前者90%的时间,而后者只有当我真正关心的特定页面的性能或更可能是因为在同一对象上做很多的操作,使后者实际上更自然。

In ASP.NET, using <%#DataBinder.Eval(Container.DataItem, "SomeProperty")%> is easier but generally less performant than <#((SomeType)Container.DataItem).SomeProperty%> or <%#SomeCodeBehindProvidedCallWithTheSameResult%>. I'll still use the former 90% of the time, and the latter only if I really care about a given page's performance or more likely because doing many operations on the same object makes the latter actually more natural.

因此,在所有的,一切都保持慢中的计算机,而它们是由要求限定在消耗的能量和需要时间的方式的单一的宇宙的工作,为的慢一些值。不同的反射技术有不同的成本,但这样做的替代品。谨防没有那么多的情况下,反射的地方它是显而易见的方法为隐藏反思的地方另一种方法稍稍不太明显的可以作为很好的。

So in all, everything remains "slow" in computers while they are bounded by the requirement to work in a single universe in a way that consumes energy and takes time, for some value of "slow". Different reflection techniques have different costs, but so does the alternatives. Beware not so much of reflection in cases where it's the obvious approach as hidden reflection where another approach just slightly less obvious may serve well.

当然,代码明智地与任何技术使用。如果你要调用相同的委托一百倍一排,你应该保存它,而不是获取它每次调用,这将要走的道路是否获取是通过反射或没有。

And of course, code wisely with whatever technique you use. If you're going to call the same delegate a hundred times in a row, you should be storing it rather than obtaining it each call, and that would go whether the way to obtain it was through reflection or not.

这篇关于反思是很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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