Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Too much code to put in a comment - the short answer is that Exists + Write should generally take longer than just write (even for existing files). </p> <p>Disk IO is not very predictable (caching, warm-up, machine load, IO queues, HDD/SSD model, etc) but running tests with a large number of iterations (more than 1000) that take more than a few ms should give you and idea. On my machine, Exists+Write generally take longer but there are exceptions too - it could be a page swap interfering or one of the VMs, who knows....</p> <p>Here's a slightly modified test suite with 4 scenarios: 1. new folder, write only 2. new folder, exists + write 3. existing folder and files (from step 2) write only 4. existing folder and files (from step 2) exists + write </p> <p>Code below:</p> <pre><code>class FTest { static string myPath = "C:\\Users\\DRook\\Desktop\\temp\\"; static string myPathFile = myPath + "file"; public static void test() { for (int i = 0; i &lt; 5; i++) { DoSomeWork(); Console.WriteLine(" = = = = = =============== = = = = ="); } Console.ReadKey(); } public static void testX1(string path, int index) { using (StreamWriter sw = new StreamWriter(path + index.ToString() + ".txt")) { sw.Write(index.ToString()); } } public static void testX2(string path, int index) { if (!File.Exists(path + index.ToString() + ".txt")) { using (StreamWriter sw = new StreamWriter(path + index.ToString() + ".txt")) { sw.Write(index.ToString()); } } else { using (StreamWriter sw = new StreamWriter(path +"n"+ index.ToString() + ".txt")) { sw.Write(index.ToString()); } } } static void runTestMeasure(Action&lt;string, int&gt; func, int count, string message, bool cleanup) { if (cleanup) { if (Directory.Exists(myPath)) Directory.Delete(myPath, true); System.Threading.Thread.Sleep(500); Directory.CreateDirectory(myPath); System.Threading.Thread.Sleep(500); } System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); for (int i = 0; i &lt; count; i++) { func(myPath,i); } stopWatch.Stop(); Console.WriteLine(message+": " + stopWatch.Elapsed); } static void DoSomeWork() { int count = 10000; runTestMeasure((path, ndx) =&gt; { testX1(path, ndx); },count,"Write missing file",true); System.Threading.Thread.Sleep(5000); runTestMeasure((path, ndx) =&gt; { testX2(path, ndx); }, count, "Write+Exists missing file",true); System.Threading.Thread.Sleep(5000); runTestMeasure((path, ndx) =&gt; { testX2(path, ndx); }, count, "Write existing file", false); System.Threading.Thread.Sleep(5000); runTestMeasure((path, ndx) =&gt; { testX2(path, ndx); }, count, "Write+Exists existing file", false); } } </code></pre> <p>Check for yourself and see how it behaves on your machine. BTW: no point in having <code>i++;</code> inside for loops.</p> <p>Edit: fixed <code>textX2</code> code to create new file (alternate name) if file exists</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload