I was trying to find an IO bottleneck in my class and surprisingly noticed that sw.Write("m"); sw.Flush()
can be 20000 faster then await sw.WriteAsync("m"); Await sw.FlushAsync();
when writing 1000000 messages to a file. Does, by any chance, anyone know why? My bet is StreamWriter
's constructor taking a String
does not parametrize a stream for async
usage.
The code below can be launched from C# interactive. Yes, it's not the best place to measure the speed but it will show the matter at hand anyway:
var sr = new StreamWriter("G:\\file.file");
var N = 1000;
var sw = new Stopwatch();
sw.Start();
for (var i = 0; i < N; ++i)
{
sr.Write("m"); // or await sr.WriteAsync("m");
sr.Flush(); // or await sr.FlushAsync("m");
}
sw.Stop();
Console.WriteLine("Completed " + N
+ " iterations in " + sw.ElapsedMilliseconds + " milliseconds.");
sr.Close();
Launched on my home PC from C# Interactive the output is
Completed 1000 iterations in 1 milliseconds.
for synchronous code and
Completed 1000 iterations in 43383 milliseconds.
for asynchronous.
Update: Also I've noticed that the program slows down inside FlushAsync
. WriteAsync
works nearly at the same speed as the synchronous version.
All comments are welcome.
Update 2*: As @Cory Nelson mentioned in comments to his answer, FileStream.FlushAsync
is a fake async
that is implemented with Task.Factory.StartNew
, so it adds nothing useful but overhead. When working with short messages the overhead becomes large enough compared to the work being done, thus slowing the execution.