Parallel 是C#的一个并行类,近期正好用到,简单试验一下其性能配置。LZ电脑配置 CPU:i7-6700U,205GHZ。分别比较了Parallel.For和普通for的遍历耗时。代码如下
byte[] imageData = new byte[2048 * 2048 * 2];
byte[] imageDarkNoise = new byte[2048 * 2048 * 2];
for (int i = 0; i < imageData.Count(); i++)
{
imageData[i] = 10;
imageDarkNoise[i] = 1;
}
Stopwatch st = new Stopwatch();
st.Start();
//for (int j = 0; j < imageData.Count(); j = j + 2)
//{
// int temp = imageData[j + 1] * 256 + imageData[j] - imageDarkNoise[j + 1] * 256 - imageDarkNoise[j];
// if (temp < 0)
// temp = 0;
// imageData[j + 1] = (byte)(temp / 256);
// imageData[j] = (byte)(temp % 256);
//}
Parallel.For(0, imageData.Count() / 2, (int j) =>
{
int temp = imageData[2 * j + 1] * 256 + imageData[2 * j] - imageDarkNoise[2 * j + 1] * 256 - imageDarkNoise[2 * j];
if (temp < 0)
temp = 0;
imageData[2 * j + 1] = (byte)(temp / 256);
imageData[2 * j] = (byte)(temp % 256);
});
Utility.LogControl.UI(st.ElapsedMilliseconds.ToString());
结果:普通for:耗时150-170ms
Parallel.For :耗时 40-50ms。性能提升还是很明显的