Max*_*ime 7 .net vb.net performance
我正在开发的应用程序运行得太慢了.
我已经运行了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
Run Code Online (Sandbox Code Playgroud)
让我更加困惑的是,在"被调用的功能"面板中,我读到了,在经历的包容时间方面:
由于该功能除了调用ToUpper和GetHashCode函数之外什么都不做,我很难搞清楚我可以在这里改进什么.
你能帮我解释一下吗?
我对VS性能诊断不是很熟悉.但是这里有关于功能体的提及.
Function Body还会显示在函数体中花费的总时间(以及时间百分比),不包括在调用和调用函数中花费的时间
但它并没有真正解释为什么2/3的时间花费在GetHashCode拨打ToUpper和GetHashCode被排除的时间.
然而..
"函数体中的高值可能表明函数本身存在性能瓶颈"
很明显,ToUpper总是必须为它必须比较的每个字符串创建一个新字符串.如果你这样做了数百万次你有很高的记忆压力并且GC开始了.这就是为什么我会使用StringComparer:
Public Overrides Function GetHashCode() As Integer
Return StringComparer.InvariantCultureIgnoreCase.GetHashCode(Name)
End Function
Run Code Online (Sandbox Code Playgroud)
你也可以使用它 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
81 次 |
| 最近记录: |