title: Checking if File.Exists() improves write speed
body: <p>My application writes some files to disc but I've realised I'm over writing existing files during this process. So, I need to check if the file exists first and then perform some logic.</p>
  
  <p>There could be many files and as such, I wanted to gauge how much over head (in terms of time) the impact would be. So, I created a console application to test it.</p>
  
  <p>My code</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  
  namespace TimeForFileRead
  {
      class Program
      {
          static string myPath = "C:\\Users\\DRook\\Desktop\\temp\\";
          static string myPathFile = myPath + "file";
          static void Main(string[] args)
          {
              for (int i = 0; i &lt; 5; i++)
              {
                  DoSomeWork();
                  Console.WriteLine(" =  =  =  =  =  =============== =  =  =  =  =");
              }
              Console.ReadKey();
          }
  
          static void DoSomeWork()
          {
              if (!Directory.Exists(myPath))
                  Directory.CreateDirectory(myPath);    
  
              System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
  
              stopWatch.Start();
  
              for (int i = 0; i &lt; 1000; i++)
              {
                  using (StreamWriter sw = new StreamWriter(myPathFile + i.ToString() + ".txt"))
                  {
                      sw.Write(i.ToString());
                  }
                  i++;
              }
  
              stopWatch.Stop();
  
              Console.WriteLine("Write only: " + stopWatch.Elapsed);
  
              Directory.Delete(myPath, true);
              System.Threading.Thread.Sleep(500);
              Directory.CreateDirectory(myPath);
              System.Threading.Thread.Sleep(500);
  
              stopWatch.Reset();
  
              stopWatch.Start();
  
              for (int i = 0; i &lt; 1000; i++)
              {
                  if (!File.Exists(myPathFile + i.ToString() + ".txt"))
                  {
                      using (StreamWriter sw = new StreamWriter(myPathFile + i.ToString() + ".txt"))
                      {
                          sw.Write(i.ToString());
                      }
                  }
                  i++;
              }
              stopWatch.Stop();
              Console.WriteLine("Write and File check: " + stopWatch.Elapsed);
          }
      }
  }
  </code></pre>
  
  <p>So, as you can see, it performs 2 actions. I is writing files to disk, the other is to check if the file already exists and if it doesn't, then write to disc.</p>
  
  <p>A screen shot of my console window (the results):</p>
  
  <p><img src="https://i.stack.imgur.com/aaZS7.png" alt="enter image description here"></p>
  
  <p>As you can see, the strange thing is that it is nearly always quicker to first check if the file exists and then write it than it is to write direct to disc. This has left me confused. Surely this makes no sense. Why does this extra over head improve the speed (considering that the <code>File.Exists()</code> will always return false in my code so therefore the Write is not skipped)? I assume a fault in my code but I've looked at this for a while and I can't make sense of it.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>As per the comments, I changed the order around a little, so I now perform the one with <code>File.Exists()</code> check first and then the write only. The results are more exaggerated (although I am now iterating over 10000 instead of 1000 as per the code above):</p>
  
  <p><img src="https://i.stack.imgur.com/qA2ok.png" alt="enter image description here"></p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>@MatthewWatson noted a fault with my code, I've updated it to ensure the directory is always deleted first. Same issue persists but at a greatly reduced occurrence yet a more dramatic difference in speed.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  
  namespace TimeForFileRead
  {
      class Program
      {
          static string myPath = "C:\\Users\\DRook\\Desktop\\temp\\";
          static string myPathFile = myPath + "file";
          static void Main(string[] args)
          {
              for (int i = 0; i &lt; 5; i++)
              {
                  DoSomeWork();
                  Console.WriteLine(" =  =  =  =  =  =============== =  =  =  =  =");
              }
              Console.ReadKey();
          }
  
          static void DoSomeWork()
          {
              if (Directory.Exists(myPath))
                  Directory.Delete(myPath, true);
  
              Directory.CreateDirectory(myPath);
  
              System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
  
              stopWatch.Start();
  
              for (int i = 0; i &lt; 10000; i++)
              {
                  using (StreamWriter sw = new StreamWriter(myPathFile + i.ToString() + ".txt"))
                  {
                      sw.Write(i.ToString());
  
                  }
                  i++;
              }
  
              stopWatch.Stop();
  
              Console.WriteLine("Write  took : " + stopWatch.Elapsed);
  
              Directory.Delete(myPath, true);
              System.Threading.Thread.Sleep(500);
              Directory.CreateDirectory(myPath);
              System.Threading.Thread.Sleep(500);
  
              stopWatch.Reset();
  
              stopWatch.Start();
  
              for (int i = 0; i &lt; 10000; i++)
              {
                  if (!File.Exists(myPathFile + i.ToString() + ".txt"))
                  {
                      using (StreamWriter sw = new StreamWriter(myPathFile + i.ToString() + ".txt"))
                      {
                          sw.Write(i.ToString());
                      }
                  }
                  i++;
              }
  
              stopWatch.Stop();
  
              Console.WriteLine("Write and check took: " + stopWatch.Elapsed);
          }
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Y8gsD.png" alt="enter image description here"></p>