【发布时间】:2021-07-31 15:17:33
【问题描述】:
我尝试将我的 for 循环更改为并行循环,但它的速度慢得多,而不是在一分钟内完成循环,而是在 30 分钟内完成。循环的作用是从一个数字开始检查它是奇数还是偶数。如果是奇数,则乘以 3 并加 1。如果是偶数,则将其除以 2。继续重复直到数字达到 4,并且有一个循环,每次重复一百万次,数字高一。我提到的最后一个循环是我尝试更改为并行循环的循环。下面是普通 for 循环的代码:
static void Main(string[] args)
{
BigInteger currenthighest =new BigInteger(Math.Pow(2,68));
BigInteger currentValue;
Console.WriteLine(currenthighest);
Console.ReadKey();
for (int i = 1; i > -1; i++)
{
for (int z = 0; z != 1000000; z++)
{
currentValue = currenthighest;
while (currentValue != 4)
{
if (currentValue % 2 == 0)
{
currentValue = currentValue / 2;
}
else
{
currentValue = (currentValue * 3) + 1;
}
}
currenthighest++;
}
Console.WriteLine(" {0} times done", i * 1000000);
}
}
这里是并行代码:
static void Main(string[] args)
{
BigInteger currenthighest =new BigInteger(Math.Pow(2,68));
BigInteger currentValue;
Console.WriteLine(currenthighest);
Console.ReadKey();
for (int i = 1; i > -1; i++)
{
Parallel.For(0, 1000000,z=>
{
currentValue = currenthighest;
while (currentValue != 4)
{
if (currentValue % 2 == 0)
{
currentValue = currentValue / 2;
}
else
{
currentValue = (currentValue * 3) + 1;
}
}
currenthighest++;
});
Console.WriteLine(" {0} times done", i * 1000000);
}
}
有人可以帮我让它比普通的 for 循环更快,或者在这种情况下使用并行是愚蠢的,我应该只使用普通的 for 循环吗?如果我也将感谢任何帮助使正常的 for 循环更快。
【问题讨论】:
-
除了速度较慢之外,并行版本也不正确,因为
currenthighest++操作is not thread-safe。 -
我的假设是线程中的工作量小于上下文切换的成本。
-
相关:Multiplying arrays element-wise has unexpected performance in C#。这个问题表明,当
Parallel.For因为工作负载太细化而达不到要求时,您可以通过切换到Parallel.ForEach(Partitioner.Create(0, 1000000)来对其进行分块。另请注意,Parallel.For/Parallel.ForEach方法在未配置MaxDegreeOfParallelism的情况下使用时,会使ThreadPool、which is bad 饱和。
标签: c# loops for-loop parallel-processing console