Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a possible solution to measure the average of all previously finished tasks. After each task finishes, an <code>Action&lt;T&gt;</code> is called where you could summarize all times and divide it by the total tasks finished. This is however just the current state and has no way to predict any future tasks / averages. (As others mentioned, this is quite difficult)</p> <p>However: You'll have to measure if it fits for your problem because there is a possibility for lock contention on both the method level declared variables.</p> <pre><code> static void ComputeParallelForWithTLS() { var collection = new List&lt;int&gt;() { 1000, 2000, 3000, 4000 }; // values used as sleep parameter var sync = new object(); TimeSpan averageTime = new TimeSpan(); int amountOfItemsDone = 0; // referenced by the TPL, increment it with lock / interlocked.increment Parallel.For(0, collection.Count, () =&gt; new TimeSpan(), (i, loopState, tlData) =&gt; { var sw = Stopwatch.StartNew(); DoWork(collection, i); sw.Stop(); return sw.Elapsed; }, threadLocalData =&gt; // Called each time a task finishes { lock (sync) { averageTime += threadLocalData; // add time used for this task to the total. } Interlocked.Increment(ref amountOfItemsDone); // increment the tasks done Console.WriteLine(averageTime.TotalMilliseconds / amountOfItemsDone + ms."); /*print out the average for all done tasks so far. For an estimation, multiply with the remaining items.*/ }); } static void DoWork(List&lt;int&gt; items, int current) { System.Threading.Thread.Sleep(items[current]); } </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. This table or related slice is empty.
    1. 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