Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get max performance using Parallel.For/ForEach? (performance timings included)
    primarykey
    data
    text
    <p>I am trying to parallelize my web parsing tool but the speed gains seem very minimal. I have i7-2600K (8 cores hyper-threading).</p> <p>Here is some code to show you the idea. I only show <code>Parallel.ForEach</code> but you get the idea:</p> <pre><code>List&lt;string&gt; AllLinks = this.GetAllLinks(); ConcurrentDictionary&lt;string, Topic&gt; AllTopics = new ConcurrentDictionary&lt;string, Topic&gt; ( ); int count = 0; Stopwatch sw = new Stopwatch ( ); sw.Start ( ); Parallel.ForEach ( AllLinks, currentLink =&gt; { Topic topic = this.ExtractTopicData ( currentLink ); this.AllTopics.TryAdd ( currentLink, topic ); ++count; if ( count &gt; 50 ) { Console.WriteLine ( sw.ElapsedMilliseconds ); count = 0; } } ); </code></pre> <p>I get these timings:</p> <pre><code>Standard foreach loop: 24582 59234 82800 117786 140315 2 links per second Paralel.For: 21902 31649 41168 49817 59321 5 links per second Paralel.ForEach: 10217 20401 39056 49220 58125 5 links per second </code></pre> <p>Firstly why is the "startup" timing is much slower in <code>Parallel.For</code>?</p> <p>Other than that the parallel loops give me 2.5x speed over the standard foreach loop. Is this normal?</p> <p>Is there a setting I can set so that the parallel loops can use all the cores?</p> <p>EDIT:</p> <p>Here is pretty much what <code>ExtractTopicData</code> does:</p> <pre><code>HtmlAgilityPack.HtmlWeb web = new HtmlWeb ( ); HtmlAgilityPack.HtmlDocument doc = web.Load ( url ); IEnumerable&lt;HtmlNode&gt; links = doc.DocumentNode.SelectNodes ( "//*[@id=\"topicDetails\"]" ); var topic = new Topic(); foreach ( var link in links ) { //parse the link data } </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.
 

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