为什么公共领域比性能更快? [英] Why are public fields faster than properties?

查看:167
本文介绍了为什么公共领域比性能更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XNA闲逛,看到其中的的Vector3 类使用的公共领域,而不是性能。我尝试了快速基准,结果发现,对于结构不同的是(将两个矢量相加100万次了2.0S具有属性和1.4S与字段)相当显着。对于引用类型,差异似乎并不是很大,但它的存在。

I was poking around in XNA and saw that the Vector3 class in it was using public fields instead of properties. I tried a quick benchmark and found that, for a struct the difference is quite dramatic (adding two Vectors together a 100 million times took 2.0s with properties and 1.4s with fields). For a reference type, the difference doesn't seem to be that large but it is there.

那么,为什么会这样?我知道一个属性被编译成 get_X set_X 方法,这将产生一个方法调用的开销。但是,不要这些简单的getter / setter方法​​的总是的获得内衬由JIT?我知道你不能保证什么JIT决定做,但肯定这是相当高的概率就行了?还有什么,从机器级的属性分开公共领域?

So why is that? I know that a property is compiled into get_X and set_X methods, which would incur a method call overhead. However, don't these simple getters/setters always get in-lined by the JIT? I know you can't guarantee what the JIT decides to do, but surely this is fairly high on the list of probability? What else is there that separates a public field from a property at the machine level?

还有一件事我一直在想:怎么是一个自动实现的属性(公众诠释美孚{获取;设置;} )'好'的面向对象设计比公共领域?或者更好的说:怎么是这两个的不同的的?我知道,使它成为一个属性是与反思,但别的更容易?我敢打赌,回答这两个问题是一回事。

And one thing I've been wondering: how is an auto-implemented property (public int Foo { get; set; }) 'better' OO-design than a public field? Or better said: how are those two different? I know that making it a property is easier with reflection, but anything else? I bet the answer to both questions is the same thing.

BTW:我使用.NET 3.5 SP1,我相信没有在内衬修复的问题,其中有结构的方法(或方法的的结构,我不知道),这样ISN T吧。我想我使用它,至少,它肯定安装,但话又说回来,我使用Vista的64位与应具备DX10.1只是我没有DX10.1 SP1。

BTW: I am using .NET 3.5 SP1 which I believe fixed issues where methods with structs (or methods of structs, I'm not sure) weren't in-lined, so that isn't it. I think I am using it at least, it's certainly installed but then again, I'm using Vista 64-bit with SP1 which should have DX10.1 except that I don't have DX10.1 ..

另外:是的,我一直在运行一个发布版本:)

Also: yeah, I've been running a release build :)

修改:我AP preciate的快速解答的家伙,但我表示我的的知道,一个属性访问是一个方法调用,但我不知道为什么,presumably,在林立的方法比直接字段访问速度较慢。

EDIT: I appreciate the quick answers guys, but I indicated that I do know that a property access is a method call, but that I don't know why the, presumably, in-lined method is slower than a direct field access.

编辑2 :所以我创造了另一个结构所用明确的getX()方法(O如何我不想念我的Java天所有的),并执行相同的我无论是在衬里上禁用(通过 [MethodImplAttribute(MethodImplOptions.NoInlining)] )或没有,所以结论:非静态方法显然从不内联,即使不是结构。

EDIT 2: So I created another struct that used explicit GetX() methods (o how I don't miss my Java days at all) and that performed the same whether I disabled in-lining on it (through [MethodImplAttribute(MethodImplOptions.NoInlining)]) or not, so conclusion: non-static methods are apparently never inlined, not even on structs.

我认为也有例外,那里的JIT可以租期,提高虚拟方法调用了。为什么不能这样呢对哪知道没有继承,因此一个方法调用只能指向一个可能的方法结构,对不对?或者是,因为你可以在其上实现一个接口?

I thought that there were exceptions, where the JIT could optmize the virtual method call away. Why can't this happen on structs which know no inheritance and thus a method call can only point to one possible method, right? Or is that because you can implement an interface on it?

这是怎样的一个耻辱,因为它真的让我想起上使用性能的关键性能的东西,但使用字段让我觉得脏,我还不如写什么我C.正在做

This is kind of a shame, since it will really make me think about using properties on performance critical stuff, yet using fields makes me feel dirty and I might as well write what I'm doing in C.

编辑3 :我发现中张贴有关完全相同的主题。他最终的结论是,物业呼叫并未得到优化。我还可以发誓,我已经读过很多次,这么简单的getter / setter属性将内衬得,尽管IL是 callvirt 。所以我要去疯了吗?

EDIT 3: I found this posting about the exact same subject. His end conclusion is that the property call did get optimized away. I also could've sworn that I've read plenty of times that simple getter/setter properties will get in-lined, despite being callvirt in the IL. So am I going insane?

修改4 :里德·科普塞张贴的答案在下面评论:

EDIT 4: Reed Copsey posted the answer in a comment below:

回复:EDIT3 - 看到我更新的评论:我相信这是86 JIT VS 64 JIT的问题。在64位的JIT并不成熟。我期望MS为更多的64位系统是天天在线过来了迅速提高。 - 里德•科普塞

Re: Edit3 - see my updated comment: I believe this is x86 JIT vs x64 JIT issues. the JIT in x64 is not as mature. I'd expect MS to improve this quickly as more 64 bit systems are coming online every day. – Reed Copsey

和我对他的答案回应:

谢谢,这就是答案!我试图迫使x86的版本和所有的方法都是一样快,而且比64快得多。这很让我震惊其实,我根本不知道我是生活在​​石器时代我的64位操作系统。我会包括我的回答您的评论,因此脱颖而出更好。 - JulianR

Thanks, this is the answer! I tried forcing a x86 build and all methods are equally fast, and much faster than the x64. This is very shocking to me actually, I had no idea I was living in the stone age on my 64-bit OS.. I'll include your comment in my answer so it stands out better. – JulianR

谢谢大家!

推荐答案

编辑2:

我这里有另一个潜在的想法:

I had another potential thought here:

您提到您在x64上运行。我测试过在x86上同样的问题,并使用自动属性与字段时看到了同样的性能。但是,如果你在连接和邮件列表/论坛的帖子看看周围,有许多参考网上的事实,64 CLR的JIT是一个不同的code基地,具有非常不同的性能特征的的x86 JIT。我的猜测是,这是一个地方,64仍然落后。

You mentioned that you are running on x64. I've tested this same issue on x86, and seen the same performance when using auto-properties vs. fields. However, if you look around on Connect and mailing list/forum posts, there are many references online to the fact that the x64 CLR's JIT is a different code base, and has very different performance characteristics to the x86 JIT. My guess is this is one place where x64 is still lagging behind.

另外,仅供参考,在结构/方法/等固定在.NET 3.5SP1的事情是在x86一边,是一个事实,即把结构作为参数的方法调用将被内联在x86到.net3之前。 5sp1。这就是你的系统上pretty很多这里的问题无关。

Also, FYI, the struct/method/etc thing fixed in .net 3.5sp1 was on the x86 side, and was the fact that method calls that took structs as a parameter would never be inlined on x86 prior to .net3.5sp1. That's pretty much irrelevant to this discussion on your system.


编辑3:

另一件事:至于为什么XNA使用领域。我其实是在游戏巨星,他们宣布XNA。波多黎各马里亚尼给他带来了许多是在他的博客积分相同的谈话。看来XNA乡亲类似的想法,当他们开发的一些核心对象。参见:

Another thing: As to why XNA is using fields. I actually was at the Game Fest where they announced XNA. Rico Mariani gave a talk where he brought up many of the same points that are on his blog. It seems the XNA folks had similar ideas when they developed some of the core objects. See:

<一个href=\"http://blogs.msdn.com/ricom/archive/2006/09/07/745085.aspx\">http://blogs.msdn.com/ricom/archive/2006/09/07/745085.aspx

特别检查点#2。


至于为什么自动属性比公共领域更好的:

As for why automatic properties are better than public fields:

他们允许你改变你的类V2的实施,并添加到逻辑根据需要在属性get / set程序,而无需改变你的界面给最终用户。这可能对你保持你的图书馆以及code在时间的能力产生深远的影响。

They allow you to change the implementation in v2 of your class, and add logic into the property get/set routines as needed, without changing your interface to your end users. This can have a profound effect on your ability to maintain your library and code over time.

----从原来的职位 - 但发现这个不是问题--------

---- From original post - but discovered this wasn't the issue--------

是你的VS之外运行的发布版本的?这可能是为什么事情没有被优化的一个解释。通常情况下,如果你是在VS,甚至优化的发布版本中运行时,VS主机进程禁用JIT的许多功能。这可能会导致性能基准来改变。

Were you running a release build outside of VS? That can be one explanation for why things aren't being optimized. Often, if you are running in VS, even an optimized release build, the VS host process disables many functions of the JIT. This can cause performance benchmarks to change.

这篇关于为什么公共领域比性能更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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