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 ...
随机推荐
- spring mvc中的valid
当你希望在spring mvc中直接校验表单参数时,你可以采用如下操作: 声明Validator的方式: 1.为每一个Controller声明一个Validator @Controller publi ...
- HDU2955Robberies(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=2955 题目是说一小偷偷东西,第i个物品的价值是M[i],被抓的概率是p[i],现在要使得在被抓的概率在P以下时的 ...
- jquery的clone方法bug的修复
最近发现jquery的clone的bug,textarea和select的jquery的clone方法有问题,textarea和select的值clone的时候会丢掉,在网上发现一个插件,下载地址如下 ...
- C#: 方法的默认参数
大家都知道在C++中,我们可以为方法的参数指定一个默认值,像这样: void foo(int i = 100); 当我们以这种形式调用方法的时候: foo(); 实际上参数i被赋于了默认值,所以相当于 ...
- hdu 5407
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5407 题意:给定一个数n,求LCM(C(n,0),C(n,1),C(n,2)...,C(n,n)) 根 ...
- 利用HTML5开发Android(3)---Android中的调试
通过JS代码输出log信息 Js代码 Js代码: console.log("Hello World"); Log信息: Console: Hello World http://ww ...
- Java常见排序算法之归并排序
在学习算法的过程中,我们难免会接触很多和排序相关的算法.总而言之,对于任何编程人员来说,基本的排序算法是必须要掌握的. 从今天开始,我们将要进行基本的排序算法的讲解.Are you ready?Let ...
- The source attachment does not contain the source for the file SignatureParser.class错误
在myeclipse整合tomcat的完毕后,再启动tomcat的时候会出现这样的错误,呵呵,错误的大致意思是什么相关联错误,其实是myeclipse新加入的tomcat的模式出现错误了,myecli ...
- Emit Mapper官方文档
概述 优点 快速指导 类型转换 用户配置
- codechef Jewels and Stones 题解
Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...