开发者

Progress update in Parallel.For() loops

开发者 https://www.devze.com 2023-01-21 20:21 出处:网络
As you can guess the index of the Parallel.For() loop jumps from one value to the other开发者_如何学C. How can I estimate the amount of the work done?

As you can guess the index of the Parallel.For() loop jumps from one value to the other开发者_如何学C. How can I estimate the amount of the work done?

Thanks.


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)


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号