title: StreamWriter: (Write+Flush)Async seem much slower then synchronous variants
body: <p>I was trying to find an IO bottleneck in my class and surprisingly noticed that <code>sw.Write("m"); sw.Flush()</code> can be 20000 faster then <code>await sw.WriteAsync("m"); Await sw.FlushAsync();</code> when writing 1000000 messages to a file. Does, by any chance, anyone know why? My bet is <code>StreamWriter</code>'s constructor taking a <code>String</code> does not parametrize a stream for <code>async</code> usage.</p>
  
  <p>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:</p>
  
  <pre><code>var sr = new StreamWriter("G:\\file.file");
  var N = 1000;
  var sw = new Stopwatch();
  sw.Start();
  
  for (var i = 0; i &lt; 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();
  </code></pre>
  
  <p>Launched on my home PC from C# Interactive the output is </p>
  
  <blockquote>
    <p>Completed 1000 iterations in 1 milliseconds.</p>
  </blockquote>
  
  <p>for synchronous code and</p>
  
  <blockquote>
    <p>Completed 1000 iterations in 43383 milliseconds.</p>
  </blockquote>
  
  <p>for asynchronous.</p>
  
  <p><strong><em>Update</em></strong>: Also I've noticed that the program slows down inside <code>FlushAsync</code>. <code>WriteAsync</code> works nearly at the same speed as the synchronous version.</p>
  
  <p>All comments are welcome.</p>
  
  <p><strong>Update 2*</strong>: As @Cory Nelson mentioned in comments to his answer, <code>FileStream.FlushAsync</code> is a fake <code>async</code> that is implemented with <code>Task.Factory.StartNew</code>, 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.</p>