C# ReadAllBytes Performance
To test the performance of File.ReadAllBytes I created a 32MB file containing binary data and a tested a series of buffer sizes ranging up to 40KB. In this instance, ReadAllBytes was nearly 40x faster than any of the buffers that were implemented.
The only reason to not use ReadAllBytes is if memory consumption is an issue, in which case you would want to read the file in buffered chunks.
Results:
Top 10 Times
Length of file: 32068000 B
buffer size elapsed time
32068000 B 16
37608 B 599
24008 B 606
8808 B 607
34408 B 609
20808 B 612
22408 B 612
33608 B 612
30408 B 613
39208 B 613
Buffered read code:
static byte[] readBytes(string file_name, int buffer)
{
List<byte[]> dirtybytes = new List<byte[]>();
using (BinaryReader b = new BinaryReader(File.Open(file_name, FileMode.Open, FileAccess.Read)))
{
//hold position counters
int pos = 0;
int length = (int)b.BaseStream.Length;
while (pos < length)
{
//read the bytes
dirtybytes.Add(b.ReadBytes(buffer));
//increment the position
pos += buffer;
}
}
//get the complete byte array
return dirtybytes.SelectMany(x => x).ToArray();
}
Written by Teddy Garland
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Buffer
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#