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 ...
随机推荐
- SFTPTool 和 FTPTooL.java
两个工具类依赖的jar包: FTPTool.java public static void main(String[] args) throws Exception{ FTPTooL ftpTool ...
- LightOJ 1112 Curious Robin Hood (单点更新+区间求和)
http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i 将第i个数值输出,并将第i个值清0 2 i v ...
- 一种将Region转为Polyline的方法
在AutoCAD.NET二次开发中,如果要将面域转为Polyline主要有以下几种方式: 1.使用Explode将面域炸成Line和Arc,然后再串起来,此方法可用于AutoCAD2007开始的所有版 ...
- Cisco 防止SYN Flood 攻击原理
DoS(Denial of Service拒绝服务)和DDoS(Distributed Denial of Service分布式拒绝服务)攻击是大型网站和网络服务器的安全威胁之一.2000年2月,Ya ...
- Linux应用总结:自动删除n天前日志
linux是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种爽快的事情.不用你去每天惦记着是否需要清理日志 ...
- CCF 201312-2 ISBN号码 (水题)
问题描述 每一本正式出版的图书都有一个ISBN号码与 之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后 ...
- POJ 3169 Layout (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...
- SQLServer: 无法修改表
工具—选项—Designers—表设计器和数据库设计器—阻止保存要求重新创建表的更改前的勾去掉.
- APK扩展文件及使用
转自:http://blog.csdn.net/myarrow/article/details/7760579 一.APK扩展文件基本知识 Android Market (Google Play St ...
- 安装nodejs和grunt以后出现 /usr/bin/env: node: No such file or directory
安装完成以后需要执行此命令 sudo ln -s /usr/bin/nodejs /usr/bin/node