VB   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了.net – 为什么“功能体”瓶颈我的应用程序?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发的应用程序运行得太慢了.

我已经运行了Visual studio的性能诊断程序,并发现一个单独的函数在66%的时间运行,即以下类的GetHashCode函数.

Public Class Identifier

    Public Property Name As String

    Public Overrides Function GetHashCode() As Integer
        Return Name.ToUpper().GetHashCode()
    End Function

    Public Overrides Function Equals(other As Object) As Boolean
        Dim otherIdentifier = TryCast(other,Identifier)
        If otherIdentifier Is Nothing Then
            Return false
        Else
            Return String.Equals(Name,otherIdentifier.Name,StringComparison.InvariantCultureIgnoreCasE)
        End If
    End Function
End Class

让我更加困惑的是,在“被调用的功能”面板中,我读到了,在经历的包容时间方面:

> System.String.ToUpper():0.61%
> System.String.GetHashCode():0.21%
>功能体:66.67%

由于函数除了调用ToUpper和GetHashCode函数之外什么都不做,我很难搞清楚我在这里可以改进什么.

你能帮我解释一下吗?

我对VS性能诊断不是很熟悉.但是 here是关于功能体的内容.

但它并没有真正解释为什么在排除对ToUpper和GetHashCode的调用时,在GetHashCode中花费了2/3的时间.

然而..

很明显,ToUpper总是必须为它必须比较的每个字符串创建一个新字符串.如果你这样做了数百万次,你就会有很高的内存压力并且GC会启动.这就是为什么我会使用StringComparer:

Public Overrides Function GetHashCode() As Integer
    Return StringComparer.InvariantCultureIgnoreCase.GetHashCode(Name)
End Function

您也可以在Equals中使用它

Public Overrides Function Equals(other As Object) As Boolean
   Dim otherIdentifier = TryCast(other,Identifier)
   If otherIdentifier Is Nothing Then Return false
   Return StringComparer.InvariantCultureIgnoreCase.Equals(Name,otherIdentifier.Name)
End Function

大佬总结

以上是大佬教程为你收集整理的.net – 为什么“功能体”瓶颈我的应用程序?全部内容,希望文章能够帮你解决.net – 为什么“功能体”瓶颈我的应用程序?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。