.NET4.0 并行循环效率对比测试

LeaderTech_NJ 2012-04-13 12:27:43
做了个.NET4.0 并行循环效率对比测试,发现并行效率居然比普通循环还要低。
请大家看看,是我代码问题还是微软忽悠人?
class Program
{
static void Main(string[] args)
{
Stopwatch timer1 = new Stopwatch();
Stopwatch timer2 = new Stopwatch();
timer1.Start();
int max=10000000;
ConcurrentQueue<int> intQueue = new ConcurrentQueue<int>();
ConcurrentQueue<int> intQueueParallel = new ConcurrentQueue<int>();
for (int i = 0; i < max; i++)
{
intQueue.Enqueue(i);
}
//List< int> intList=intQueue.ToList<int>();
timer1.Stop();
Console.WriteLine("普通循环共耗时:" + timer1.Elapsed.TotalSeconds + "秒");

timer2.Start();
Parallel.For(0, max, (i) => intQueueParallel.Enqueue(i));
//List<int> intListParallel = intQueueParallel.ToList<int>();
timer2.Stop();
Console.WriteLine("并行共耗时:"+timer2.Elapsed.TotalSeconds+"秒");
Console.ReadKey();
}
}


normal:1.22s
parallel:1.61s
...全文
309 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhddzz 2012-04-14
  • 打赏
  • 举报
回复
好歹看下MSDN再开始干啊

当 For() 循环的循环体很小时,它的执行速度可能比等效的顺序循环更慢。 对数据进行分区所涉及的开销以及调用每个循环迭代上的委托的开销导致了性能降低。 为了解决类似情况,Partitioner 类提供 Create 方法,该方法使您可以为委托体提供顺序循环,以便每个分区只调用一次委托,而不是每个迭代调用一次委托。 有关更多信息,请参见 PLINQ 和 TPL 的自定义分区程序。

http://msdn.microsoft.com/zh-cn/library/dd560853.aspx
cheng2005 2012-04-13
  • 打赏
  • 举报
回复
要找对合适的模型再进行测试,比如你用单程累加一个数肯定比多线程累加要快,难道楼主想说单线程比多线程快?
startstartsvip 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:
Parallel.For效率太低了,建议楼主自己实现 Parallel.For方法

难道微软忽悠人?
[/Quote]

不是微软忽悠人,是你的例子不好,你放个延迟看看





class Program
{
static void Main(string[] args)
{
Stopwatch timer1 = new Stopwatch();
Stopwatch timer2 = new Stopwatch();
timer1.Start();
int max=10000000;
ConcurrentQueue<int> intQueue = new ConcurrentQueue<int>();
ConcurrentQueue<int> intQueueParallel = new ConcurrentQueue<int>();
for (int i = 0; i < max; i++)
{
sleep(10);
intQueue.Enqueue(i);
}
//List< int> intList=intQueue.ToList<int>();
timer1.Stop();
Console.WriteLine("普通循环共耗时:" + timer1.Elapsed.TotalSeconds + "秒");

timer2.Start();
Parallel.For(0, max, (i) => {
Thread.Sleep(10);
intQueueParallel.Enqueue(i);
});
//List<int> intListParallel = intQueueParallel.ToList<int>();
timer2.Stop();
Console.WriteLine("并行共耗时:"+timer2.Elapsed.TotalSeconds+"秒");
Console.ReadKey();
}
}

cheng2005 2012-04-13
  • 打赏
  • 举报
回复
楼主的例子就好像说
多线程比单线程慢一样。
anzhiqiang_touzi 2012-04-13
  • 打赏
  • 举报
回复
Parallel.For(0, max, (i) => intQueueParallel.Enqueue(i));

表达式是需要动态编译和解析数据的
gomoku 2012-04-13
  • 打赏
  • 举报
回复
其实微软的文章已经指出任务的粒度对并行任务的影响。

如果任务很小,那么由于并行管理的附加开销(任务分配,调度,同步等成本),可能并行执行并不是优化方案。

你任务只是一个很小的Enqueue,试试把它写成如下任务,然后再作比较:
(i) => {intQueueParallel.Enqueue(i); Thread.Sleep(1000);}
LeaderTech_NJ 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
Parallel.For效率太低了,建议楼主自己实现 Parallel.For方法
[/Quote]
难道微软忽悠人?
yyz985 2012-04-13
  • 打赏
  • 举报
回复
Parallel.For效率太低了,建议楼主自己实现 Parallel.For方法
startstartsvip 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

我重新做了个测试:
C# code
class Program
{
static void Main(string[] args)
{
Stopwatch timer1 = new Stopwatch();
Stopwatch timer2 = new Stopwatch();
……
[/Quote]

sleep(10)你把 max改成 1000 就行了
LeaderTech_NJ 2012-04-13
  • 打赏
  • 举报
回复
我重新做了个测试:
  class Program
{
static void Main(string[] args)
{
Stopwatch timer1 = new Stopwatch();
Stopwatch timer2 = new Stopwatch();
timer1.Start();
int max = 100000;
ConcurrentQueue<int> intQueue = new ConcurrentQueue<int>();
ConcurrentQueue<int> intQueueParallel = new ConcurrentQueue<int>();
for (int i = 0; i < max; i++)
{
if (Is_prime(i))
intQueue.Enqueue(i);
}
//List< int> intList=intQueue.ToList<int>();
timer1.Stop();
Console.WriteLine("普通循环共耗时:" + timer1.Elapsed.TotalSeconds + "秒");

timer2.Start();
Parallel.For(0, max, (i) =>
{
if (Is_prime(i))
intQueueParallel.Enqueue(i);
});
//List<int> intListParallel = intQueueParallel.ToList<int>();
timer2.Stop();
Console.WriteLine("并行共耗时:"+timer2.Elapsed.TotalSeconds+"秒");
Console.ReadKey();
}

static bool Is_prime(int n)
{
if (n < 2)
return false;
for (int i = 2; i < n; ++i)
{
if ((n % i) == 0)
return false;
}
return true;
}




}


result:
normal:7.16
Parallel:2.77
这次测试看起来正常多了。初步结论看来是要加入一定的计算复杂度才能体现并行的优势。
我不太理解为什么要加个sleep(10)?
我测了下加个sleep(10)貌似更慢了

111,100

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧