我决定使用 1,000,000 个对象的集合来测试每种方法的性能。对象是以下之一:整数、类、Nothing 或 DBNull.Value。每次测试都使用相同的集合,我测试了每种方法 50 次。
"" & x
这实际上不适用于所有对象。它适用于 DBNull.Value 和 Nothing,但尝试将此方法与任何 ol' 对象一起使用将导致 InvalidCastException。有趣的是,CStr(DBNull.Value) 会抛出 InvalidCastException,所以我不确定它为什么会起作用。
使用自定义对象的结果:不适用 没有自定义对象的
结果:平均126.7 毫秒,中位数126 毫秒
If(x, "").ToString()
使用自定义对象的结果:平均140.46 毫秒,中位数138 毫秒
没有
自定义对象的结果:平均69.32 毫秒,中位数69 毫秒
Convert.ToString()
使用自定义对象的结果:平均171.54 毫秒,中位数171 毫秒
没有
自定义对象的结果:平均112.14 毫秒,中位数112 毫秒
因此,对于非常大的记录集来说,这似乎If(x, "").ToString()
有点快,但这需要与Convert.ToString()
's 更强大的转换选项相平衡。感谢您的回答。
这是我用于测试的代码:
Option Strict Off
Module Module1
Sub Main()
Dim l_objectArray = Enumerable.Range(0, 1000000).Select(Function(x) GetObject(x)).ToArray()
Dim l_stopWatch As New Stopwatch()
Dim l_testResults As New List(Of Long)
Dim l_testIterations As Integer = 50
Dim l_displayValue As String
Do
' --------------------
'Console.WriteLine()
'Console.WriteLine("Conversion using string concatenation")
'l_testResults.Clear()
'For iteration = 0 To l_testIterations - 1
' l_stopWatch.Start()
' For Each o In l_objectArray
' l_displayValue = "" & o
' Next
' l_stopWatch.Stop()
' l_testResults.Add(l_stopWatch.ElapsedMilliseconds)
' l_stopWatch.Reset()
'Next
'Console.WriteLine()
'Console.WriteLine("Average: " & l_testResults.Average())
'Console.WriteLine("Median: " & GetMedian(l_testResults.ToArray()))
' --------------------
Console.WriteLine()
Console.WriteLine("Conversion using Object.ToString()")
l_testResults.Clear()
For iteration = 0 To l_testIterations - 1
l_stopWatch.Start()
For Each o In l_objectArray
l_displayValue = If(o, "").ToString()
Next
l_stopWatch.Stop()
l_testResults.Add(l_stopWatch.ElapsedMilliseconds)
l_stopWatch.Reset()
Next
Console.WriteLine()
Console.WriteLine("Average: " & l_testResults.Average())
Console.WriteLine("Median: " & GetMedian(l_testResults.ToArray()))
' --------------------
Console.WriteLine()
Console.WriteLine("Conversion using Convert.ToString(x)")
l_testResults.Clear()
For iteration = 0 To l_testIterations - 1
l_stopWatch.Start()
For Each o In l_objectArray
l_displayValue = Convert.ToString(o)
Next
l_stopWatch.Stop()
l_testResults.Add(l_stopWatch.ElapsedMilliseconds)
l_stopWatch.Reset()
Next
Console.WriteLine()
Console.WriteLine("Average: " & l_testResults.Average())
Console.WriteLine("Median: " & GetMedian(l_testResults.ToArray()))
' --------------------
Console.WriteLine()
Console.Write("Exit? (y/n): ")
Dim l_key = Console.ReadKey(False)
If l_key.Key = ConsoleKey.Y Then
Exit Sub
End If
Loop
End Sub
Private Function GetMedian(ByVal values As Long()) As Long
Array.Sort(values)
If values.Length Mod 2 = 0 Then
Return (values(values.Length / 2) + values(values.Length / 2 - 1)) / 2
Else
Return values(CInt(Math.Floor(values.Length / 2)))
End If
End Function
Private Function GetObject(ByVal someNumber As Integer) As Object
Select Case someNumber Mod 4
Case 0
Return someNumber
Case 1
Return New SomeClass(someNumber)
'Return Nothing
Case 2
Return DBNull.Value
Case Else
Return Nothing
End Select
End Function
Private Class SomeClass
Private _seed As Integer
Public Sub New(ByVal seed As Integer)
_seed = seed
End Sub
Public Overrides Function ToString() As String
Return _seed.ToString()
End Function
End Class
End Module