Does any one know of a faster method to do String.Split()?

后端 未结 14 1163
傲寒
傲寒 2020-12-03 10:57

I am reading each line of a CSV file and need to get the individual values in each column. So right now I am just using:

values = line.Split(delimiter);
         


        
14条回答
  •  醉梦人生
    2020-12-03 11:08

    This is my solution:

    Public Shared Function FastSplit(inputString As String, separator As String) As String()
            Dim kwds(1) As String
            Dim k = 0
            Dim tmp As String = ""
    
            For l = 1 To inputString.Length - 1
                tmp = Mid(inputString, l, 1)
                If tmp = separator Then k += 1 : tmp = "" : ReDim Preserve kwds(k + 1)
                kwds(k) &= tmp
            Next
    
            Return kwds
    End Function
    

    Here is a version with benchmarking:

    Public Shared Function FastSplit(inputString As String, separator As String) As String()
            Dim sw As New Stopwatch
            sw.Start()
            Dim kwds(1) As String
            Dim k = 0
            Dim tmp As String = ""
    
            For l = 1 To inputString.Length - 1
                tmp = Mid(inputString, l, 1)
                If tmp = separator Then k += 1 : tmp = "" : ReDim Preserve kwds(k + 1)
                kwds(k) &= tmp
            Next
            sw.Stop()
            Dim fsTime As Long = sw.ElapsedTicks
    
            sw.Start()
            Dim strings() As String = inputString.Split(separator)
            sw.Stop()
    
            Debug.Print("FastSplit took " + fsTime.ToString + " whereas split took " + sw.ElapsedTicks.ToString)
    
            Return kwds
    End Function
    

    Here are some results on relatively small strings but with varying sizes, up to 8kb blocks. (times are in ticks)

    FastSplit took 8 whereas split took 10

    FastSplit took 214 whereas split took 216

    FastSplit took 10 whereas split took 12

    FastSplit took 8 whereas split took 9

    FastSplit took 8 whereas split took 10

    FastSplit took 10 whereas split took 12

    FastSplit took 7 whereas split took 9

    FastSplit took 6 whereas split took 8

    FastSplit took 5 whereas split took 7

    FastSplit took 10 whereas split took 13

    FastSplit took 9 whereas split took 232

    FastSplit took 7 whereas split took 8

    FastSplit took 8 whereas split took 9

    FastSplit took 8 whereas split took 10

    FastSplit took 215 whereas split took 217

    FastSplit took 10 whereas split took 231

    FastSplit took 8 whereas split took 10

    FastSplit took 8 whereas split took 10

    FastSplit took 7 whereas split took 9

    FastSplit took 8 whereas split took 10

    FastSplit took 10 whereas split took 1405

    FastSplit took 9 whereas split took 11

    FastSplit took 8 whereas split took 10

    Also, I know someone will discourage my use of ReDim Preserve instead of using a list... The reason is, the list really didn't provide any speed difference in my benchmarks so I went back to the "simple" way.

提交回复
热议问题