Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing task parallel library in IEnumerable implementation to achieve speed improvement
    text
    copied!<p>Following code is simplified version of the code that I am trying to optimize. </p> <pre><code>void Main() { var words = new List&lt;string&gt; {"abcd", "wxyz", "1234"}; foreach (var character in SplitItOut(words)) { Console.WriteLine (character); } } public IEnumerable&lt;char&gt; SplitItOut(IEnumerable&lt;string&gt; words) { foreach (string word in words) { var characters = GetCharacters(word); foreach (char c in characters) { yield return c; } } } char[] GetCharacters(string word) { Thread.Sleep(5000); return word.ToCharArray(); } </code></pre> <p>I cannot change the signature of method SplitItOut.The GetCharacters method is expensive to call but is thread safe. The input to SplitItOut method can contain 100,000+ entries and a single call to GetCharacters() method can take around 200ms. It can also throw exceptions which I can ignore. Order of the results do not matter.</p> <p>In my first attempt I came up with following implementation using TPL which speeds up the things quite a bit, but is blocking till I am done processing all the words.</p> <pre><code>public IEnumerable&lt;char&gt; SplitItOut(IEnumerable&lt;string&gt; words) { Task&lt;char[][]&gt; tasks = Task&lt;char[][]&gt;.Factory.StartNew(() =&gt; { ConcurrentBag&lt;char[]&gt; taskResults = new ConcurrentBag&lt;char[]&gt;(); Parallel.ForEach(words, word =&gt; { taskResults.Add(GetCharacters(word)); }); return taskResults.ToArray(); }); foreach (var wordResult in tasks.Result) { foreach (var c in wordResult) { yield return c; } } } </code></pre> <p>I am looking for any better implementation for method SplitItOut() than this. Lower processing time is my priority here.</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