Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress update in Parallel.For() loops

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

Thanks.

like image 878
abenci Avatar asked Oct 21 '10 09:10

abenci


2 Answers

By keeping a counter rather than looking at the index? For example:

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

(the Interlocked usage is essential to avoid getting race-conditions on accessing counter)

like image 196
Marc Gravell Avatar answered Nov 16 '22 14:11

Marc Gravell


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

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

like image 33
Andrey Avatar answered Nov 16 '22 14:11

Andrey