为什么地产执行而非字段或方法执行慢? [英] Why is Property execution slower than Field or Method execution?

查看:153
本文介绍了为什么地产执行而非字段或方法执行慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CLR通过CSHARP 第10章属性杰夫里氏写道:

In CLR via CSharp chapter 10 "Properties" Jeff Richter writes:

一个属性方法可能需要很长的时间来执行;场获得总   立即完成。使用性能的一个常见原因是   执行线程同步采样,它可以永远地停止线程,   因此,一个属性不应如果线程中使用   同步是必需的。在这种情况下,一种方法是preferred。   此外,如果你的类可以被远程访问(例如,你的类   从System.MarshalByRefObject派生),调用的属性   法将是非常慢的,因此,一种方法是pferred到$ P $   属性。在我看来,类派生自MarshalByRefObject   不应该使用的属性。

A property method can take a long time to execute; field access always completes immediately. A common reason to use properties is to perform thread synchroni- zation, which can stop the thread forever, and therefore, a property should not be used if thread synchronization is required. In that situation, a method is preferred. Also, if your class can be accessed remotely (for example, your class is derived from System.MarshalByRefObject), calling the property method will be very slow, and therefore, a method is preferred to a property. In my opinion, classes derived from MarshalByRefObject should never use properties.

这是即使该属性被定义为只返回私人领域的情况?为什么一个的方法的preferred同步?为什么是的方法的preferred在MarshalByRefObject的情况下?

Is this the case even if the property is defined to just return the private field? Why is a Method preferred in synchronization? and why is a Method preferred in MarshalByRefObject scenario?

要澄清我的问题:
杰夫似乎是使一个毯子声明属性是不可取的,而且方法是preferable在这两种情景。作为乔·怀特指出,属性可以有任意code。但方法可以运行相同的任意code。这部分我遇到困难。有没有真正的优势使用过属性的方法(给予同样的code时)同步或编组,还是他仅仅有语言习惯的问题吗?

To clarify my question:
Jeff seems to be making a blanket statement that Properties are not advisable, and that methods are preferable in those 2 scenarios. as Joe White pointed out, properties can have arbitrary code. But methods can run the same arbitrary code. That's the part i'm having difficulty with. Is there actually advantage in using methods over properties (given the same code is used) for synchronization or marshaling, or does he merely have a problem with language convention?

推荐答案

我觉得他做的,因为一个属性可以运行任意code点时,的调用code 的不要以为它会立即结束。

I think he's making the point that, because a property can run any arbitrary code, the calling code shouldn't assume that it will finish immediately.

如果所有的属性确实是返回一个字段,那么它的方法体将实际上是由JIT编译器内联,这将是一样快的字段访问。所以,这并不是说性能在某种程度上慢;那就是他们是黑盒子。如果你不知道一个属性是如何实现的,你不能假设它返回快。

If all the property does is return a field, then its method body will actually be inlined by the JIT compiler and it'll be just as fast as a field access. So it's not that properties are somehow slower; it's that they're black boxes. If you don't know how a property is implemented, you can't make assumptions about it returning quickly.

(也就是说,迈着缓慢性会明显违反了 .NET框架设计指南的< /一>,特别是这一个:你用的方法,而不是一个属性,[如果]操作要比一个字段设置将是更慢的指令)

(That said, making a slow property would be a clear violation of the .NET Framework Design Guidelines, specifically this one: "Do use a method, rather than a property, [if the] operation is orders of magnitude slower than a field set would be".)

至于他的使用方法,而不是建议,我不能做出任何意义。属性的的方法:属性getter是一个方法(通常名为 get_PropertyName ),和属性setter是一个方法( set_PropertyName )和code,读取属性被编译为code,使一个方法调用 get_PropertyName 。没有什么特别的,这将使属性不是方法的任何慢。

As for his suggestion of using methods instead, I can't make any sense of that. Properties are methods: the property getter is a method (typically named get_PropertyName), and the property setter is a method (set_PropertyName), and code that reads the property is compiled to code that makes a method call to get_PropertyName. There's nothing special that would make a property any slower than a method.

这篇关于为什么地产执行而非字段或方法执行慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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