I made some test to mesure the c# read perfomance on binary file and I
made some curious discovery. Except for some minor details, the
following is the code I used to read the file:
byte[] buffer = new byte[bufferSize];
FileStream fileStream = new FileStream(fileName,
FileMode.Open,
FileAccess.Read, FileShare.None, fileStreamBufferSize,
fileOptions);
BinaryReader stream = new BinaryReader(fileStream);
int itemRead;
while (true)
{
itemRead = stream.Read(buffer, 0, bufferSize);
if (itemRead == 0) //end of file
break;
}
The interesting things happen playing with the fileOptions parameter.
Its type is an enumeration and twos of the admitted value are:
* FileOptions.SequentialScan
* FileOptions.None
I ran the above code consecutively 8 times on a 100MB files varying
fileOptions value. Here it is the results according the FileOptions
value:
1 FileOptions.SequentialScan: 20.05 MB/Sec
2 FileOptions.SequentialScan: 20.25 MB/Sec
3 FileOptions.SequentialScan: 20.40 MB/Sec
4 FileOptions.SequentialScan: 20.39 MB/Sec
5 FileOptions.None: 16.55 MB/Sec
6 FileOptions.None: 704.23 MB/Sec
7 FileOptions.SequentialScan: 680.27 MB/Sec
8 FileOptions.SequentialScan: 694.44 MB/Sec
I expected to see the cache effect show up starting from the second
read but this does not happen until sixth read. I repeated the test
many times and the system cache never (with the exception I will
discuss further) start to cache at first read. In my tests caching
starts only when I open the file using FileOptions.None. After that
moment all further read take advantage of the cache system. Any idea
what's happening? I made some more test and collect them here:
http://www.dragoncello.com/blog/
g.