I was testing some file related functionality and wanted to know what the raw file write performance on my system was. So I wrote a small test method like the one below:
public static TimeSpan PerformTest(long fileSize, int bufferSize)
{
var buffer = new byte[bufferSize];
var loops = fileSize / bufferSize;
var path = @"C:\Test\Test.data";
if (!Directory.Exists(Path.GetDirectoryName(path)))
Directory.CreateDirectory(Path.GetDirectoryName(path));
using (var file = new FileStream(path, FileMode.Create))
{
var stopwatch = Stopwatch.StartNew();
for (var idx = 0; idx < loops; idx++)
{
file.Write(buffer, 0, bufferSize);
}
stopwatch.Stop();
return stopwatch.Elapsed;
}
}I call it like this:
var elapsed = PerformTest(1024 * 1024 * 1024, 8 * 1024 * 1024);
In all cases I passed in 1GB as the file size (1st parameter). However I'm seeing a huge performance difference depending on the value I pass into the second parameter. Here is an example:
1K 5.4s 8K 2.5s 32K 1.1s 64K 0.9s 256K 0.7s 512K 0.7s 1M 0.7s 8M 0.8s 16M 0.8s 32M 6.1s 64M 8.3s 128M 9.7s 256M 9.7s 512M 9.9s 1G 11s
Unless I did something stoopid in my test method (very likely), why am I seeing such slow performance when passing in a large buffer into the Write call? The overall amount of data is still exactly the same in all cases. I would think it would already break up the buffer into multiple writes internally and should not really see much slowdown if a larger buffer is passed in.
Any ideas?
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.