Search code examples
c#stringperformancetextpinvoke

Fast text reading (alternatives to File.ReadAllText() and/or StreamReader.ReadToEnd())


Quick-read question: I was wondering if there'd be other techniques that I overlooked, maybe p/invoke to a certain library (be it winapi or third party library). All advice is welcome.

The full context of the question: For a given usecase I need to read textfiles into memory which I afterwards can manipulate. The problem lies not in the manipulation though but in the I/O. I'm currently using following techniques withing C#:

1) ReadAllText() method of "File"

var content = File.ReadAllText(file.FullName);

2) ReadToEnd() method of "StreamReader"

var content = String.Empty;
using(var streamReader = File.OpenText(file.FullName)) {
    content = streamReader.ReadToEnd();
}

3) I also tried using a BufferedStream in conjunction with method 2

All had roughly the same performance for files between 5 to 20MB. So, here comes the question then: I was wondering if there'd be other techniques that I overlooked, maybe p/invoke to a certain library (be it winapi or third party library). All advice is welcome.


Solution

  • The bottleneck for all of the variants that you list will be the I/O. Any method to read a complete file from disk into memory will meet the same bottleneck.

    Therefore, it is reasonable to conclude that no alternative approach will yield significant gains. For sure you will find slight differences in performance between these and other methods. But you are never going to see orders of magnitude gains.