Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As ChrisBint suggested, use a parallel loop.</p> <p>I created three methods to write the files (code below). One uses your code above, one uses File.WriteAllBytes(...) - both these use a traditional for loop.</p> <p>The third implementation used a parallel for loop.</p> <p>Here are the times to create 1000 files:</p> <blockquote> <p>FileStream: 2658ms</p> <p>File.WriteAllBytes: 2555ms </p> <p>Parallel.For: 617ms</p> </blockquote> <p>So the parallel loop is four times faster than the slowest implementation. Obviously this will vary on different hardware, and your results will depend very much on your CPU and disk.</p> <p>Here's the code:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Diagnostics; using System.Threading.Tasks; namespace FileCreator { class Program { static void Main(string[] args) { string folder = @"d:\temp"; Clean(folder); CreateWithParallelFileWriteBytes(folder); Clean(folder); CreateWithFileStream(folder); Clean(folder); CreateWithFileWriteBytes(folder); } private static void Clean(string folder) { if (Directory.Exists(folder)) { Directory.Delete(folder, true); } Directory.CreateDirectory(folder); } private static byte[] GetFileContent(int i) { Random r = new Random(i); byte[] buffer = new byte[1024]; r.NextBytes(buffer); return buffer; } private static void CreateWithFileStream(string folder) { var sw = new Stopwatch(); sw.Start(); for (int i = 0; i &lt; 1000; i++) { string path = Path.Combine(folder, string.Format("file{0}.dat", i)); using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.None)) { byte[] bytes = GetFileContent(i); fs.Write(bytes, 0, bytes.Length); } } Console.WriteLine("Time for CreateWithFileStream: {0}ms", sw.ElapsedMilliseconds); } private static void CreateWithFileWriteBytes(string folder) { var sw = new Stopwatch(); sw.Start(); for (int i = 0; i &lt; 1000; i++) { string path = Path.Combine(folder, string.Format("file{0}.dat", i)); File.WriteAllBytes(path, GetFileContent(i)); } Console.WriteLine("Time for CreateWithFileWriteBytes: {0}ms", sw.ElapsedMilliseconds); } private static void CreateWithParallelFileWriteBytes(string folder) { var sw = new Stopwatch(); sw.Start(); Parallel.For(0, 1000, (i) =&gt; { string path = Path.Combine(folder, string.Format("file{0}.dat", i)); File.WriteAllBytes(path, GetFileContent(i)); }); Console.WriteLine("Time for CreateWithParallelFileWriteBytes: {0}ms", sw.ElapsedMilliseconds); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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