为什么并行代码更慢? [英] Why parallel code is slower?
本文介绍了为什么并行代码更慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用大数组创建了一个简单的测试,我得到了并行 for 和普通 for 之间的巨大差异
I created a simple test with big arrays, and I get a big difference between parallel for and normal for
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var rn = new Random(541);
var tmStart = DateTime.Now;
int[,] a = new int[2048, 2048];
for (int i = 0; i < 2048; i++)
for (int j = 0; j < 2048; j++)
a[i, j] = rn.Next();
var tmEnd = DateTime.Now - tmStart;
Console.WriteLine("Normal..: {0}",tmEnd.ToString());
tmStart = DateTime.Now;
a = new int[2048, 2048];
Parallel.For(0, 2048, i =>
{
for (int j = 0; j < 2048; j++)
a[i, j] = rn.Next();
});
tmEnd = DateTime.Now - tmStart;
Console.WriteLine("Parallel: {0}", tmEnd.ToString());
}
}
}
处理时间:
正常..:00:00:00.1250071
并行:00:00:00.3880222
Normal..: 00:00:00.1250071
Parallel: 00:00:00.3880222
为什么差别这么大?
我想象如果你使用多个线程,它会更快......
I imagined that if you use several threads, it would be faster...
推荐答案
您的基准测试存在一些问题:
There are a few things wrong with your benchmark:
- 您的任务非常小,线程开销很大.
- 您使用
Random
的实例打破了线程规则.谁知道这有什么影响? - 你的时机很粗糙.
- Your tasks are so small that the threading overhead is significant.
- You break the threading rules with the instance of
Random
. Who knows what impact this has? - Your timing is, well, crude.
这是一个更好的基准测试,显示代码的并行版本速度更快:
Here's a better benchmark that shows the parallel version of the code being faster:
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
const int N = 1024;
static void Main(string[] args)
{
int[,] a = new int[N, N];
var stopwatch = Stopwatch.StartNew();
for (int i = 0; i < N; i++)
for (int k = 0; k < N; k++)
for (int j = 0; j < N; j++)
a[i, j] = i+j;
Console.WriteLine("Normal..: {0}", stopwatch.ElapsedMilliseconds);
stopwatch = Stopwatch.StartNew();
Parallel.For(0, N, i =>
{
for (int k = 0; k < N; k++)
for (int j = 0; j < N; j++)
a[i, j] = i+j;
});
Console.WriteLine("Parallel: {0}", stopwatch.ElapsedMilliseconds);
Console.ReadLine();
}
}
}
这是使用随机数生成器的一个:
And here's one which uses random number generators:
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
const int N = 512;
static void Main(string[] args)
{
int[,] a = new int[N, N];
var stopwatch = Stopwatch.StartNew();
var rng = new Random();
for (int i = 0; i < N; i++)
for (int k = 0; k < N; k++)
for (int j = 0; j < N; j++)
a[i, j] = rng.Next();
Console.WriteLine("Normal..: {0}", stopwatch.ElapsedMilliseconds);
stopwatch = Stopwatch.StartNew();
Parallel.For(0, N, i =>
{
var rngpar = new Random();
for (int k = 0; k < N; k++)
for (int j = 0; j < N; j++)
a[i, j] = rngpar.Next();
});
Console.WriteLine("Parallel: {0}", stopwatch.ElapsedMilliseconds);
Console.ReadLine();
}
}
}
这篇关于为什么并行代码更慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文