简体   繁体   English

在 C# 中定义自定义类型的属性时是否存在任何性能问题?

[英]Is there any performace issue when defining a property of custom type in C#?

Say I have four classes A,B,C and D.假设我有四个类 A、B、C 和 D。

D has property: Name(string) D 具有属性:名称(字符串)

C has property: D1(type D) C 具有属性:D1(D 型)

B has property: C1(type C) B有属性:C1(C型)

A has property B1(type B) A有属性B1(B型)

With A1 as instance of A, I can access: A1.B1.C1.D1.Name使用 A1 作为 A 的实例,我可以访问: A1.B1.C1.D1.Name

Is there any performance issue with this?这有任何性能问题吗?

How deep can a property residing won't be any huge performance hit?一个房产的居住深度有多深不会受到任何巨大的性能影响?

Or is there a limit to this hierarchy?或者这个层次结构有限制吗?

There is no performance issue, it can go as deep as you want (to a certain limit if I remember correctly. However, if you hit that, you have other, more pressing, problems).没有性能问题,它可以 go 尽可能深(如果我没记错的话,可以达到一定的极限。但是,如果你遇到了其他更紧迫的问题)。

Also, while going down the object graph isn't really expensive, arbitrary code in the getters might be.此外,虽然沿着 object 图表并不昂贵,但 getter 中的任意代码可能会很昂贵。

If all the properties in the chain are sealed (the default) and the getters are trivial, then the JITter will probably be able to inline the chain of calls.如果链中的所有属性都是密封的(默认)并且 getter 是微不足道的,那么 JITter 可能能够内联调用链。 You will incur a sequence of pointer lookups, so this technically won't be as fast as if A had a copy of Name, but the overhead is unlikely to be significant.您将产生一系列指针查找,因此从技术上讲,这不会像 A 拥有 Name 的副本那样快,但开销不太可能很大。

If any of the properties in the chain are virtual, then I believe the JITter will not be able to inline the calls, and you will incur the overhead of one or more function calls (the property gets).如果链中的任何属性是虚拟的,那么我相信 JITter 将无法内联调用,并且您将承担一个或多个 function 调用的开销(属性获取)。 That is still extremely small though (again, still assuming the getters are trivial).尽管如此,这仍然非常小(同样,仍然假设吸气剂是微不足道的)。

As always, the only way to be sure is to measure.与往常一样,唯一确定的方法是测量。 And be conscious of what you are measuring: if the nested chain turns out to be, say, 50% slower than A having its own copy of Name, that doesn't mean it's a big deal, unless your program spends a large amount of its runtime on that Name get -- highly unlikely!并且要注意你正在测量的内容:如果嵌套链比拥有自己的 Name 副本的 A 慢 50%,这并不意味着它有什么大不了的,除非你的程序花费大量它在该名称上的运行时间得到 - 极不可能! So do the right thing first -- make the program readable and maintainable first -- and if you measure and find it to be a bottleneck, then look at optimising it.所以首先做正确的事——首先让程序可读和可维护——如果你测量并发现它是一个瓶颈,然后考虑优化它。

With some quick profiling, on my machine a ++ on a trivial property that is one level deep runs in 3 nanoseconds, the same property when nested 10 deep runs in 6 nanoseconds... so if you are doing this a couple million times a second it might be something to look at, but in most cases its going to be insignificant.通过一些快速分析,在我的机器上,一个++在一个微不足道的属性上,即在 3 纳秒内运行一级深度,在 6 纳秒内嵌套 10 次深度运行时,相同的属性......所以如果你这样做了几百万次其次,它可能值得一看,但在大多数情况下,它是微不足道的。

Given that you don't over do, no it's not an issue.鉴于您没有过度使用,不,这不是问题。 In real world scenarios object oriented programming requires these type of properties, for eg person.personalDetails.address.city在现实世界的场景中,面向 object 的编程需要这些类型的属性,例如 person.personalDetails.address.city

I haven't been doing any .net for a while now, so take my answer with the due doubts.我已经有一段时间没有做任何 .net 了,所以带着适当的疑问来回答我的问题。 I think the compiler should optimize the getter - unless it's doing something very complicated.我认为编译器应该优化 getter - 除非它正在做一些非常复杂的事情。 What's left is the penalty you pay for jumping to a different address in memory.剩下的就是跳转到 memory 中不同地址的惩罚。 As an example, your objects in memory could be in any order, like this one: D [other data] C BA So, for each get you're asking for an address backward in memory, which is likely to cause small slowdowns and very likely page faults (this is also true in other cases).例如,您在 memory 中的对象可以按任何顺序排列,如下所示: D [其他数据] C BA 因此,对于每个 get,您都要求在 ZCD69B4957F06CD818D7BF3D61980 中向后追溯地址,这可能会导致速度下降并且非常小,可能的页面错误(在其他情况下也是如此)。 However, this is only to answer your question.但是,这只是为了回答您的问题。 In practice, you hardly need to take this into account.在实践中,您几乎不需要考虑这一点。 Unless you're writing some inner loop of a time-critical and very complex algorithm, the difference in execution time is really close to 0.除非您正在编写一些时间紧迫且非常复杂的算法的内部循环,否则执行时间的差异实际上接近于 0。

A best practice in such a case if you need to use your string very often is to store a reference to it and use that reference instead of calling your chain of properties.在这种情况下,如果您需要经常使用字符串,最佳做法是存储对它的引用并使用该引用而不是调用您的属性链。 If you don't gain anything in speed, at least you get code that is easier to read, to type and to maintain.如果您没有在速度上获得任何好处,那么至少您会获得更易于阅读、键入和维护的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM