Parallel.ForEach() 并行循环
现在的电脑几乎都是多核的,但在软件中并还没有跟上这个节奏,大多数软件还是采用传统的方式,并没有很好的发挥多核的优势。
微软的并行运算平台(Microsoft’s Parallel Computing Platform (PCP))提供了这样一个工具,让软件开发人员可以有效的使用多核提供的性能。
Parallel.ForEach()和Parallel.For()就是微软并发类的成员。
今天做了一个简单的测试,我的电脑是双核的,效果还是比较明显的。
一般的for和foreach循环用时都在10秒钟;并发for循环在0.5秒,并发foreach在0.1秒钟。
但是并发循环不能滥用,在简单的少次数循环下,并发循环可能会体现不出其优势。
下面是简单的测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace parallelForeach
{
class Program
{
static void Main(string[] args)
{
DateTime startTime;
TimeSpan resultTime;
List<entityA> source = new List<entityA>();
for (int i = ; i < ; i++)
{
source.Add(new entityA
{
name = "悟空" + i,
sex = i % == ? "男" : "女",
age = i
});
}
startTime = System.DateTime.Now;
loop1(source);
resultTime = System.DateTime.Now - startTime;
Console.WriteLine("一般for循环耗时:" + resultTime);
startTime = System.DateTime.Now;
loop2(source);
resultTime = System.DateTime.Now - startTime;
Console.WriteLine("一般foreach循环耗时:" + resultTime);
startTime = System.DateTime.Now;
loop3(source);
resultTime = System.DateTime.Now - startTime;
Console.WriteLine("并行for循环耗时:" + resultTime.Milliseconds);
startTime = System.DateTime.Now;
loop4(source);
resultTime = System.DateTime.Now - startTime;
Console.WriteLine("并行foreach循环耗时:" + resultTime.Milliseconds);
Console.ReadLine();
} //普通的for循环
static void loop1(List<entityA> source)
{
int count = source.Count();
for (int i = ; i < count; i++)
{
System.Threading.Thread.Sleep();
}
} //普通的foreach循环
static void loop2(List<entityA> source)
{
foreach (entityA item in source)
{
System.Threading.Thread.Sleep();
}
} //并行的for循环
static void loop3(List<entityA> source)
{
int count = source.Count();
Parallel.For(, count, item =>
{
System.Threading.Thread.Sleep();
});
} //并行的foreach循环
static void loop4(List<entityA> source)
{
Parallel.ForEach(source, item =>
{
System.Threading.Thread.Sleep();
});
}
} //简单的实体
class entityA
{
public string name { set; get; }
public string sex { set; get; }
public int age { set; get; }
}
}
Parallel.ForEach() 并行循环的更多相关文章
- Parallel.ForEach 使用多线遍历循环
Parallel.ForEach相对于foreach是多线程,并行操作;foreach是单线程品德操作. static void Main(string[] args) { Console.Write ...
- C# 使用Parallel并行开发Parallel.For、Parallel.Foreach实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- .NET4中多线程并行方法Parallel.ForEach
原文发布时间为:2011-12-10 -- 来源于本人的百度文章 [由搬家工具导入] namespace ForEachDemo{ using System; using System.I ...
- 并行forearch的使用及测试(Parallel.Foreach)
using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tas ...
- Parallel并行循环
Parallel.For(, , new ParallelOptions() { MaxDegreeOfParallelism = 100 },(i, pls) => { ) { pls.Bre ...
- .NET并行编程实践(一:.NET并行计算基本介绍、并行循环使用模式)
阅读目录: 1.开篇介绍 2.NET并行计算基本介绍 3.并行循环使用模式 3.1并行For循环 3.2并行ForEach循环 3.3并行LINQ(PLINQ) 1]开篇介绍 最近这几天在捣鼓并行计算 ...
- [No000088]并行循环vs普通循环
using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks ...
- Parallel.ForEach , ThreadPool.QueueUserWorkItem
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- .NET并行计算基本介绍、并行循环使用模式
.NET并行计算基本介绍.并行循环使用模式) 阅读目录: 1.开篇介绍 2.NET并行计算基本介绍 3.并行循环使用模式 3.1并行For循环 3.2并行ForEach循环 3.3并行LINQ(PLI ...
随机推荐
- thinkphp,ajax返回结果
thinkphp 在Action中 利用dump输出的是<pre>success</pre> 利用echo输出的是 success 1.html eventResize: fu ...
- C#POP3协议实现SSL验证登陆GMAIL
最近在折腾POP3协议,登陆pop.qq.com和pop.163.com没有什么问题,于是就想着登陆pop.gmail.com,结果失败了.经查,发现gmail的pop3端口不是110,而是995.于 ...
- easyui 表单和自定义验证扩展和js自定义返回值
================jsp==========================<form method="post" id="regfrminp&qu ...
- My集合框架第三弹 AVL树
旋转操作: 由于任意一个结点最多只有两个儿子,所以当高度不平衡时,只可能是以下四种情况造成的: 1. 对该结点的左儿子的左子树进行了一次插入. 2. 对该结点的左儿子的右子树进行了一次插入. 3. 对 ...
- CentOS6 下安装JDK7
1.下载JDK:http://www.oracle.com/technetwork/java/javase/archive-139210.html,选择Java SE 7,下载jdk-7u80-lin ...
- C#中利用委托实现多线程跨线程操作
在使用VS2005的时候,如果你从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常.这是微软为了保证线程安全以及提高代码的效率所做的改进,但是也给大家带来很多不便. 其实解决这个 ...
- KMP算法初探
[edit by xingoo] kmp算法其实就是一种改进的字符串匹配算法.复杂度可以达到O(n+m),n是参考字符串长度,m是匹配字符串长度. 传统的算法,就是匹配字符串与参考字符串挨个比较,如果 ...
- sc7731 Android 5.1 LCD驱动简明笔记之一
基于展讯sc7731 - Android 5.1 代码分析浏览.将屏蔽细节,把握整体,并且不涉及其他设备和LCD的交互. 以下对sc7731 lcd大体流程进行简要说明. 第一,lcd 的两个阶段 1 ...
- HttpRuntime.Cache 失效
最近做一个报纸内容类网站,为了提高响应速度,将首页各栏目以及二级栏目中Part文献列表存储在HttpRuntime.Cache缓存中,发布后发现问题,刚插入的缓存很快就失效,本机调试没有问题. 由于H ...
- CMSIS Example - osTimer osTimerCreate osTimerStart
osTimerId timer; uint32_t cnt=; void timerHandler( void * arg ) { cnt++; osTimerStart( timer, ); } o ...