场的实际性能与特性 [英] Actual Performance of Fields vs. Properties

查看:122
本文介绍了场的实际性能与特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一些后期生成CIL编织,增加了CIL在装配的所有方法(换句话说吨的方法)。每个方法检查,如果一个特定的值是零。例(C#Reflector'd版本的CIL代码):

I'm doing some post-build CIL weaving that adds CIL to all methods in an assembly (in other words tons of methods). Each method checks if a specific value is null. Example (C# Reflector'd version of CIL code):

// CIL woven region start
if (MyType.Something == null) {
 // ... some new stuff
}
// CIL woven region end

什么是具有MyType.Something作为地产主场迎战场的表现有何影响?我知道我读过,C#编译器进行特殊优化,并应在这种情况下,对性能没有影响......但是怎么样在直接CIL代码(没有C#编译器)的情况下...?或者是JIT编译器,允许这些优化(所以直接CIL代码仍然利益)?

What is the performance impact of having MyType.Something as a Property vs. a Field? I know I've read that the C# compiler performs special optimizations and there should be no performance impact in that case...but what about in the case of direct CIL code (no C# compiler)...? Or is it the JIT compiler that allows for these optimizations (so direct CIL code still benefits)?

会发光OpCode.Call的静态属性的访问比Ldsfld表现较差(请记住,这是跨越数万调用,因为组件中的每方法编织)?

Will emitting OpCode.Call for the static property's accessor have poorer performance than Ldsfld (bear in mind this is across tens of thousands of invocations since every method in the assembly is woven)?

感谢。

推荐答案

在开发SlimDX数学库,我们发现,在pre-.NET 3.5 SP1的框架,使用字段为数学类型的成员(如X,Y ,Z为的Vector3)给在性能不成比例的性能提升。换句话说,差异有显着为这在很大程度上访问的属性小数学函数。

When developing the math library for SlimDX, we found that, on pre-.NET 3.5 SP1 frameworks, using fields for the members of the math types (such as X, Y, Z for a Vector3) gave a disproportionate performance increase over properties. In other words, the difference was noticeable for small math functions which heavily accessed properties.

这已经被改善,因为.NET 3.5 SP1(见的 JIT inling )。虽然我相信JIT在此之前,仍将内嵌小方法(属性是简单的方法毕竟),存在这样防止了接受或返回值类型的方法内联早期的框架中的错误。

This has since been improved since .NET 3.5 SP1 (see JIT inling). While I believe that the JIT prior to that will still inline small methods (properties are simply methods after all), there is a bug in the earlier frameworks that prevented inlining of methods that take or return value types.

注意区别,当出现,还是相当小的。我仍然会选择使用的所有属性,但最关键的性能情况。

Note that the difference, when there, is still quite small. I would still elect to use properties in all but the most performance critical cases.

这篇关于场的实际性能与特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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