前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >平行运算:Parallel.For、Parallel.Foreach的体验式试用[通俗易懂]

平行运算:Parallel.For、Parallel.Foreach的体验式试用[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-09 11:38:01
7190
发布2022-09-09 11:38:01
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

在编程里面我们经常会遇到编历一个列表或数组做同一件事情或操作,当这个数组或列表很大时又或是需要进行很复杂的操作时,就会花费很长的时间。以前我就在想能不能在这种情况下使用多线程的方式提高效率,可惜一直都没机会和动力(实际需要)去研究。今天在网上查找资料,很偶然的发现.NET Framework 4.0中平行算法相关内容(Parallel.For、Parallel.Foreach),原来.NET已经实现这项功能而且语法简化的异常简单。具体内容请大家查阅参考资料,下面将贴出我的测试结果与大家共享。

PS:首先确认开发环境(VS2010+.NET Framework 4)

第一轮测试:

static void Main(string[] args) { //产生测试资料 List<int> testData = new List<int>(); Random Rand = new Random(); //产生乱数列表 for (int i = 0; i < 2147400; i++) { testData.Add(Rand.Next(1000)); } //打印正确结果 Console.WriteLine(testData.Sum());

for (int i = 0; i < 10; i++) { Console.WriteLine(); TestFor(testData); TestParallelFor(testData); TestParallelForeach(testData); } Console.ReadKey(); }

static void TestFor(List<int> testData) { DateTime time1 = DateTime.Now; //记录结果用 long resultData = 0; foreach (var item in testData) { resultData += item; } Console.WriteLine(string.Format(“ForEach: \t\t{0} in {1}”, resultData, (DateTime.Now – time1).TotalMilliseconds)); }

static void TestParallelFor(List<int> testData) { DateTime time1 = DateTime.Now; //记录结果用 long resultData = 0; Parallel.For(0, testData.Count , (i, loopState) => { resultData += testData[i]; }); Console.WriteLine(string.Format(“Parallel.For: \t{0} in {1}”, resultData, (DateTime.Now – time1).TotalMilliseconds)); }

static void TestParallelForeach(List<int> testData) { //记录结果用 DateTime time1 = DateTime.Now; long resultData = 0; Parallel.ForEach(testData, (item, loopState) => { resultData += item; }); Console.WriteLine(string.Format(“Parallel.ForEach:\t{0} in {1}”, resultData, (DateTime.Now – time1).TotalMilliseconds)); }

测试结果

测试分析结果:Foreach胜出,且Parallel.For、Parallel.Foreach计算均以错误告终,顿时让我失望不已。不过仔细一想,发现应该是平行运算时,因为是多线程同时使用resultData这个共享资源时的访问起了冲突,所以导致最后的求和失败。要想防止资源不起冲突,只能对共享资源进行加锁,但这又与平行算法的思想违背。于是乎改进方法重新测试。

第二轮测试:

static void Main(string[] args) { //产生测试资料 List<int> testData = new List<int>(); Random Rand = new Random(); //产生乱数列表 for (int i = 0; i < 2147401; i++) { testData.Add(Rand.Next(1000)); } //打印正确结果 Console.WriteLine(testData.Sum());

for (int i = 0; i < 10; i++) { Console.WriteLine(); TestFor(testData); TestParallelFor(testData); TestParallelForeach(testData); } Console.ReadKey(); }

static void TestFor(List<int> testData) { DateTime time1 = DateTime.Now; //记录结果用 List<int> resultData = new List<int>(); foreach (var item in testData) { resultData.Add(item); } Console.WriteLine(string.Format(“ForEach: \t\t{0} in {1}”, resultData.Sum(), (DateTime.Now – time1).TotalMilliseconds)); }

static void TestParallelFor(List<int> testData) { DateTime time1 = DateTime.Now; //记录结果用 ConcurrentStack<int> resultData = new ConcurrentStack<int>(); Parallel.For(0, testData.Count, (i, loopState) => { resultData.Push(testData[i]); }); Console.WriteLine(string.Format(“Parallel.For: \t{0} in {1}”, resultData.Sum(), (DateTime.Now – time1).TotalMilliseconds)); }

static void TestParallelForeach(List<int> testData) { //记录结果用 DateTime time1 = DateTime.Now; ConcurrentStack<int> resultData = new ConcurrentStack<int>(); Parallel.ForEach(testData, (item, loopState) => { resultData.Push(item); }); Console.WriteLine(string.Format(“Parallel.ForEach:\t{0} in {1}”, resultData.Sum(), (DateTime.Now – time1).TotalMilliseconds)); }

测试结果

测试分析结果:Parallel.For、Parallel.Foreach计算终于正确,这表明确实是资源访问的问题,但这个效率问题,还不如直接使用Foreach,这是怎么会事儿啊,没道理啊,怎么着我的电脑也还是个双核嘛。

再仔细分析一下,第一轮测试与第二轮的测试结果,虽然第一轮测试Parallel.For、Parallel.Foreach计算错误,但好歹执行效率上与Foreach相差不大,那么效率应该是出在了ConcurrentStack.Push上了。(这是因为在 .Net 3.5 之前所提供的所有 Collections 都不是线程安全的,必須使用.Net 4.0 , System.Collections.Concurrent Namespace 下的泛型)于是有了以下的第三轮测试~~~~

第三轮测试:

static void Main(string[] args) { //产生测试资料 List<int> testData = new List<int>(); Random Rand = new Random(); //产生乱数列表 for (int i = 0; i < 2147401; i++) { testData.Add(Rand.Next(1000)); } //打印正确结果 Console.WriteLine(testData.Sum());

for (int i = 0; i < 10; i++) { Console.WriteLine(); TestFor(testData); TestParallelFor(testData); TestParallelForeach(testData); } Console.ReadKey(); }

static void TestFor(List<int> testData) { DateTime time1 = DateTime.Now; foreach (var item in testData) { item.ToString(); } Console.WriteLine(string.Format(“ForEach: \t\t{0} in {1}”, testData.Sum(), (DateTime.Now – time1).TotalMilliseconds)); }

static void TestParallelFor(List<int> testData) { DateTime time1 = DateTime.Now; Parallel.For(0, testData.Count, (i, loopState) => { testData[i].ToString(); }); Console.WriteLine(string.Format(“Parallel.For: \t{0} in {1}”, testData.Sum(), (DateTime.Now – time1).TotalMilliseconds)); }

static void TestParallelForeach(List<int> testData) { //记录结果用 DateTime time1 = DateTime.Now; ConcurrentStack<int> resultData = new ConcurrentStack<int>(); Parallel.ForEach(testData, (item, loopState) => { item.ToString(); }); Console.WriteLine(string.Format(“Parallel.ForEach:\t{0} in {1}”, testData.Sum(), (DateTime.Now – time1).TotalMilliseconds)); }

测试结果

测试分析结果:这下子Parallel.For、Parallel.Foreach终于发挥出了平行运算的优势,将效率提高了接近一半左右。

测试总结:对于Parallel.For、Parallel.Foreach的使用应该要特别小心,它们的优势是处理列表很长,且对列表内的元素进行很复杂的业务逻辑,且不会使用共享资源,只针对自身的业务逻辑处理,方才能提升效率。因为如果逻辑过于简单的话,创建线程的花费将大于业务执行的花费,得不偿失。

参考资料

平行运算 (一):Parallel.For、Parallel.Foreach 用法及技巧

How to: Write a Simple Parallel.For Loop

How to: Write a Simple Parallel.ForEach Loop

Introducing ConcurrentStack < T >

System.Collections.Concurrent Namespace

The Parallel Programming Of .NET Framework 4.0(1) – Beginning

The Parallel Programming Of .NET Framework 4.0(2) – Task Library

The Parallel Programming Of .NET Framework 4.0(3) – Deep Into Task Library

The Parallel Programming Of .NET Framework 4.0(4) – Inside Out Of Task Library

The Parallel Programming Of .NET Framework 4.0(5) – Dive to Parallel Programming

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/161833.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 测试结果
  • 测试结果
  • 测试结果
    • 参考资料
      • 平行运算 (一):Parallel.For、Parallel.Foreach 用法及技巧
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档