Update execution in Parallel.For () loops

As you can guess, the Parallel.For () loop index jumps from one value to another. How can I estimate the amount of work done?

Thank.

+5
source share
2 answers

Keeping the counter, not looking at the index? For instance:

int counter  = 0;
Parallel.For(4, 500, i => {
    // TODO: something useful...         
    int progress = Interlocked.Increment(ref counter);
    Console.WriteLine("{0}: {1}", progress, i);
});

(use Interlockedis important to avoid getting race conditions when accessing counter)

+7
source
int progress = 0;
Parallel.For( from, to, i => {
// do the job
Interlocked.Increment(ref progress);
});

now actual progress (float)(to - from) / progress

+3
source

All Articles