Generics vs. Array Lists

后端 未结 12 1483
孤独总比滥情好
孤独总比滥情好 2020-12-10 12:19

The system I work on here was written before .net 2.0 and didn\'t have the benefit of generics. It was eventually updated to 2.0, but none of the code was refactored due to

12条回答
  •  时光说笑
    2020-12-10 12:41

    Here's the results I got from a simple parsing of a string from a 100KB file 100,000 times. The Generic List(Of char) took 612.293 seconds to go 100,000 times through the file. The ArrayList took 2,880.415 seconds to go 100,000 times through the file. This means in this scenario (as your mileage will vary) the Generic List(Of char) is 4.7 times faster.

    Here is the code I ran through 100,000 times:

    Public Sub Run(ByVal strToProcess As String) Implements IPerfStub.Run
        Dim genList As New ArrayList
    
        For Each ch As Char In strToProcess.ToCharArray
            genList.Add(ch)
        Next
    
        Dim dummy As New System.Text.StringBuilder()
        For i As Integer = 0 To genList.Count - 1
            dummy.Append(genList(i))
        Next
    
    End Sub
    
     Public Sub Run(ByVal strToProcess As String) Implements IPerfStub.Run
         Dim genList As New List(Of Char)
    
         For Each ch As Char In strToProcess.ToCharArray
             genList.Add(ch)
         Next
    
         Dim dummy As New System.Text.StringBuilder()
         For i As Integer = 0 To genList.Count - 1
             dummy.Append(genList(i))
         Next
     End Sub
    

提交回复
热议问题