如何计算并行的完成百分比
本文关键字:百分比 并行 何计算 计算 | 更新日期: 2023-09-27 17:49:38
使用for
计算完成百分比很容易:
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
// Do somethings...
Console.WriteLine("{0}% Done!", i * 10 + j + 1);
}
}
输出:1% Done!
2% Done!
3% Done!
4% Done!
5% Done!
6% Done!
7% Done!
8% Done!
9% Done!
10% Done!
11% Done!
12% Done!
13% Done!
...
93% Done!
94% Done!
95% Done!
96% Done!
97% Done!
98% Done!
99% Done!
100% Done!
但是我对Parallel.For
的完成率计算没有任何概念:
Parallel.For(0, 10, i =>
{
Parallel.For(0, 10, j =>
{
// Do somethings...
Console.WriteLine("{0}% Done!", i * 10 + j + 1);
});
});
输出:1% Done!
2% Done!
3% Done!
51% Done!
52% Done!
53% Done!
54% Done!
55% Done!
56% Done!
57% Done!
58% Done!
59% Done!
60% Done!
4% Done!
5% Done!
6% Done!
...
69% Done!
70% Done!
71% Done!
72% Done!
73% Done!
74% Done!
75% Done!
76% Done!
77% Done!
78% Done!
79% Done!
80% Done!
36% Done!
37% Done!
38% Done!
39% Done!
40% Done!
35% Done!
输出没有显示正确的完成百分比。
如何在不降低性能的情况下计算Parallel.For
的完成率?
您可以使用Interlocked.Increment
方法:
int progress = 0;
Parallel.For(0, 10, i =>
{
Parallel.For(0, 10, j =>
{
// Do somethings...
Console.WriteLine("{0}% Done!", Interlocked.Increment(ref progress));
});
});
内部循环的每次迭代都有一个增量。一些打印输出可能出现顺序混乱——特别是,当线程在获得进度的新值之后,但在打印完成之前被抢占时。
对于这样一个简单的情况,当您知道您正在处理正好100个项目时,只需使用一个外部变量(线程安全)递增并报告:
int progress = 0;
Parallel.For(0, 10, i =>
{
Parallel.For(0, 10, j =>
{
// Do somethings...
Console.WriteLine("{0}% Done!", Interlocked.Increment(ref progress));
});
});
但是请注意,您可能仍然会得到一些报告顺序错误的值(因为并行任务将执行增量和,然后必须竞争访问Console
,这是一个独占资源)