构造函数调用其他构造函数:有任何性能问题吗?

发布于 2024-11-05 17:22:00 字数 795 浏览 3 评论 0原文

在性能至关重要的应用程序中,场景 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

↙厌世 2024-11-12 17:22:00

不,不会有明显的区别。

您可以运行自己的基准测试并找出答案。

No, there wouldn't be a noticeable difference.

You can run your own benchmark and find out.

小红帽 2024-11-12 17:22:00

速度应该不会相差很大。从裸构造函数到深层构造函数仅再调用 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.

一抹微笑 2024-11-12 17:22:00

这取决于你所说的引人注目是什么意思。场景 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.)

情栀口红 2024-11-12 17:22:00

不要使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文