构造函数调用其他构造函数:有任何性能问题吗?
在性能至关重要的应用程序中,场景 1(完全独立的构造函数)与场景 2(链式调用构造函数)相比是否有任何明显的优势?
场景 1
Class TwoInts
Private a, b As Integer
Public Sub New(ByVal a As Integer, ByVal b As Integer)
Me.a = a
Me.b = b
End Sub
Public Sub New(ByVal a As Integer)
Me.a = a
Me.b = 0
End Sub
Public Sub New()
Me.a = 0
Me.b = 0
End Sub
End Class
场景 2
Class TwoInts
Private a, b As Integer
Public Sub New(ByVal a As Integer, ByVal b As Integer)
Me.a = a
Me.b = b
End Sub
Public Sub New(ByVal a As Integer)
Me.New(a, 0)
End Sub
Public Sub New()
Me.New(0)
End Sub
End Class
In an application where performance is crucial, would there be any noticeable advantage of Scenario 1 (completely separate constructors) vs. Scenario 2 (chain-calling constructors)?
Scenario 1
Class TwoInts
Private a, b As Integer
Public Sub New(ByVal a As Integer, ByVal b As Integer)
Me.a = a
Me.b = b
End Sub
Public Sub New(ByVal a As Integer)
Me.a = a
Me.b = 0
End Sub
Public Sub New()
Me.a = 0
Me.b = 0
End Sub
End Class
Scenario 2
Class TwoInts
Private a, b As Integer
Public Sub New(ByVal a As Integer, ByVal b As Integer)
Me.a = a
Me.b = b
End Sub
Public Sub New(ByVal a As Integer)
Me.New(a, 0)
End Sub
Public Sub New()
Me.New(0)
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,不会有明显的区别。
您可以运行自己的基准测试并找出答案。
No, there wouldn't be a noticeable difference.
You can run your own benchmark and find out.
速度应该不会相差很大。从裸构造函数到深层构造函数仅再调用 2 次...但是:
您应该询问 C# 语言它对调用构造函数的构造函数有何看法 :P
在那里它不会很好,所以我认为你应该坚持使用不同的方法,例如创建“初始化方法”,并从每个构造函数调用它,并有一个更可读的代码。在那里,您需要使用“:this(0)”符号,该符号不太可读(例如,读取时是程序性的)。而且它会减少一次调用来达到做某事的目的。
The speed should not be very different. Thjere are only 2 more calls from the bare constructor to the deep one... but:
You should ask the language C# what it thinks about constructors that call constructors :P
There it would not go very well, so I think you should stick with a different approach like making an "Initialization method", and call that from each constructor, and have a more readable code. There you would need to use the ": this(0)" Notation which is not so readable (eg. procedural when reading it). And it would have one less call to get to the point of doing something.
这取决于你所说的引人注目是什么意思。场景 1 确实引入了额外的调用,但它增加的时间将以毫秒为单位进行测量。场景2会更快。 (它也会增加生成代码的大小。)
It depends on what you mean by noticeable. Scenario 1 does introduce extra calls, but the time it adds will be measured in fractions of a millisecond. Scenario 2 will be faster. (It will also increase the size of the generated code.)
不要使用 Marino Šimić 提出的常见 Init() 方法!
它是C++风格,不适合C#。在 C# 中,初始化应该在构造函数中完成!
有关详细信息,请参阅 Bill Wagner 的书“Effective C#”:
http://www.diranieh。 com/NETCSharp/EffectiveCS.htm#14._Use_Constructor_Chaining
Don't use a common Init() method as proposed by Marino Šimić!
It is C++ style and not suitable for C#. In C# initialization should be done in the constructors!
For details see Bill Wagners book "Effective C#":
http://www.diranieh.com/NETCSharp/EffectiveCS.htm#14._Use_Constructor_Chaining