Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would note that most answers haven't read the example the code. This is not about spawning a bunch of threads and writing to disk, this is about spawning a bunch of threads, doing some work <em>new JavaScriptSerializer().Serialize(saeed);</em> and <strong>then</strong> writing to disk! </p> <p>This is important to note, because the longer that work takes the more benefit simple threading provides by making sure the disk isn't idling while computation takes place.</p> <hr> <p>The long and short of it is because you wrote some simplistic code, as others have explained:</p> <ol> <li>You are creating 90,000 threads - this is expensive and unnecessary! </li> <li>You are locking all the work, making this single threaded! <ol> <li>Yes without the lock you get an exception... that doesn't magically make the lock a good idea from a performance idea - it just means you've got erroneous code.</li> </ol></li> </ol> <p>A quick and easy way to get into threading - that's slightly less dangerous (though you can still stuff it up) is to use the Task Parallel Library. For example:</p> <pre><code>using System; using System.Diagnostics; using System.IO; using System.Threading.Tasks; namespace ConsoleApplication15 { class Program { const int FILE_COUNT = 9000; const int DATA_LENGTH = 100; static void Main(string[] args) { if (Directory.Exists(@"c:\Temp\")) Directory.Delete(@"c:\Temp\", true); Directory.CreateDirectory(@"c:\Temp\"); var watch = Stopwatch.StartNew(); for (int i = 0; i &lt; FILE_COUNT; i++) { string data = new string(i.ToString()[0], DATA_LENGTH); File.AppendAllText(string.Format(@"c:\Temp\{0}.txt", i), data); } watch.Stop(); Console.WriteLine("Wrote 90,000 files single-threaded in {0}ms", watch.ElapsedMilliseconds); Directory.Delete(@"c:\Temp\", true); Directory.CreateDirectory(@"c:\Temp\"); watch = Stopwatch.StartNew(); Parallel.For(0, FILE_COUNT, i =&gt; { string data = new string(i.ToString()[0], DATA_LENGTH); File.AppendAllText(string.Format(@"c:\Temp\{0}.txt", i), data); }); watch.Stop(); Console.WriteLine("Wrote 90,000 files multi-threaded in {0}ms", watch.ElapsedMilliseconds); } } } </code></pre> <p>The single threaded version runs in about 8.1 seconds, and the multi-threaded version runs in about 3.8 seconds. Note that my test values are different than yours.</p> <p>While the TPL's default settings aren't always optimised for the scenario you're working on, they provide a much better basis than running 90,000 threads! You'll also note that in this case I don't have to do any locking nor do I have to handle the closure - because the API presented already handles that for me.</p>
    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. 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