多亏了这个网站上的人们的帮助,我开始了并行编程的第一步。
下面是我目前正在测试的C# (简化)代码。
do
{
// PRN data download to the waveform generator...
// Response data acquisition from the oscilloscope...
// etc...
t0 = Win32.MM_GetTime();
// Serial evaluation of the data DFT;
// execution time constantly = 0.785 s.
//DSP.DFT_2_F(D1, ref R1, ref X1);
//DSP.DFT_2_F(D2, ref R2, ref X2);
// Parallel evaluation of the data DFT;
// execution time = 0.395 s, but...
Parallel.Invoke(
() => DSP.DFT_2_F(D1, ref R1, ref X1),
() => DSP.DFT_2_F(D2, ref R2, ref X2)
);
lblTMis.Text = ((double)(Win32.MM_GetTime() - t0) / 1000.0).ToString();
// Plot the measurement results...
// etc...
}
while (bMisuraOn);现在,虽然串行DFT计算总是需要0.785秒,而并行版本只需要0.395秒,但是时不时地,比如说每3-4次测量循环,执行时间就会跳到0.838秒。我还注意到,在50-60次循环之后,执行时间稳定到0.395秒,只有非常偶然的跳跃;如果我重新启动测量循环,而不退出程序,那么一段时间后,执行时间就会一次又一次地消失。
在我看来,有时Parallel.Invoke决定表现为串行...
这是正确的还是我做错了什么?有没有办法让执行时间总是一样(快)?
再见,谢谢你的关注。
佛朗哥
发布于 2014-06-10 22:38:55
来自Parallel.Invoke上的MSDN
可能并行地执行所提供的每个操作。
线程不能保证异步执行,它通常是异步执行的。我不确定如何进一步绕过它,但这至少解释了这种行为。
https://stackoverflow.com/questions/24143748
复制相似问题