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

后端 未结 14 1162
傲寒
傲寒 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:17

    The main problem(?) with String.Split is that it's general, in that it caters for many needs.

    If you know more about your data than Split would, it can make an improvement to make your own.

    For instance, if:

    1. You don't care about empty strings, so you don't need to handle those any special way
    2. You don't need to trim strings, so you don't need to do anything with or around those
    3. You don't need to check for quoted commas or quotes
    4. You don't need to handle quotes at all

    If any of these are true, you might see an improvement by writing your own more specific version of String.Split.

    Having said that, the first question you should ask is whether this actually is a problem worth solving. Is the time taken to read and import the file so long that you actually feel this is a good use of your time? If not, then I would leave it alone.

    The second question is why String.Split is using that much time compared to the rest of your code. If the answer is that the code is doing very little with the data, then I would probably not bother.

    However, if, say, you're stuffing the data into a database, then 66% of the time of your code spent in String.Split constitutes a big big problem.

提交回复
热议问题