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

后端 未结 7 657
名媛妹妹
名媛妹妹 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:22

    As other answers/comments have mentioned, you are probably reading the file from cache anyway so the disk/SDRAM speed is not limiting factor.

    When you parse the file, you are doing many more allocations from the heap (when you split the string, and when you add the auto-boxed parsed values to your lists) than when you just count lines. It is the cost of these heap allocations that is probably making the difference in performance between the two passes.

    You can parse faster than you can read from the disk, but parsers optimized for speed are vary careful about memory usage.

提交回复
热议问题