How to parse a text file in C# and be io bound?

后端 未结 7 687
名媛妹妹
名媛妹妹 2020-12-03 19:01

It is known that if you read data from disc you are IO bound and you can process/parse the read data much faster than you can read it from disc.

But this common wis

7条回答
  •  误落风尘
    2020-12-03 19:10

    Just couple of suggestions (if you are willing to live with more complicated implementation)...

    • You could use LinkedList instead of List to avoid re-allocation/copy when Add-ing.
    • Replace Split with handwritten code that searches for separator and then uses Substring to extract the "fields". You only need to search for a single separator and number of fields is known in advance, so Split is too general for you.
    • Use Read instead of ReadLine so you can re-use the same buffer and avoid allocating new string for every line.
    • If you are really performance-conscientious, separate parsing into concurrent Tasks. While you are at it, put the file reading into its own task as well.

提交回复
热议问题