现在的电脑几乎都是多核的,但在软件中并还没有跟上这个节奏,大多数软件还是采用传统的方式,并没有很好的发挥多核的优势。
微软的并行运算平台(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() 并行循环的更多相关文章

  1. Parallel.ForEach 使用多线遍历循环

    Parallel.ForEach相对于foreach是多线程,并行操作;foreach是单线程品德操作. static void Main(string[] args) { Console.Write ...

  2. C# 使用Parallel并行开发Parallel.For、Parallel.Foreach实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  3. .NET4中多线程并行方法Parallel.ForEach

    原文发布时间为:2011-12-10 -- 来源于本人的百度文章 [由搬家工具导入] namespace ForEachDemo{    using System;    using System.I ...

  4. 并行forearch的使用及测试(Parallel.Foreach)

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tas ...

  5. Parallel并行循环

    Parallel.For(, , new ParallelOptions() { MaxDegreeOfParallelism = 100 },(i, pls) => { ) { pls.Bre ...

  6. .NET并行编程实践(一:.NET并行计算基本介绍、并行循环使用模式)

    阅读目录: 1.开篇介绍 2.NET并行计算基本介绍 3.并行循环使用模式 3.1并行For循环 3.2并行ForEach循环 3.3并行LINQ(PLINQ) 1]开篇介绍 最近这几天在捣鼓并行计算 ...

  7. [No000088]并行循环vs普通循环

    using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks ...

  8. Parallel.ForEach , ThreadPool.QueueUserWorkItem

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. .NET并行计算基本介绍、并行循环使用模式

    .NET并行计算基本介绍.并行循环使用模式) 阅读目录: 1.开篇介绍 2.NET并行计算基本介绍 3.并行循环使用模式 3.1并行For循环 3.2并行ForEach循环 3.3并行LINQ(PLI ...

随机推荐

  1. FATFS文件系统

    STM32移植文件系统,操作SD卡,对SD卡进行读写 FATFS文件系统与底层介质的驱动分离开来,对底层介质的操作都要交给用户去实现,它仅仅是提供了一个函数接口而已,函数为空,要用户添加代码.然后 F ...

  2. python scp

    scp 0.10.2 Downloads ↓ scp module for paramiko Pure python scp module====================== The scp. ...

  3. HDU 5702 Solving Order (水题,排序)

    题意:给定几种不同的颜色和它的权值,按它的权值排序. 析:排序. 代码如下: #include <cstdio> #include <string> #include < ...

  4. ckeditor 升级到 4.5

    原来的项目用的是4.0+asp.net 3.5的,一直不错,这两天升级一下ckeditor到最新版4.5.1,用的是chrome浏览器测试,发觉TextBox.Text获取不到数据,在页面用js写do ...

  5. 转载 使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(一&二)——初识WiX

    转载fromVan Pan 的专栏   http://blog.csdn.net/rryqsh/article/details/8274832 http://blog.csdn.net/rryqsh/ ...

  6. Ruby学习资源汇总

    from:http://segmentfault.com/a/1190000000362058 Ruby 语言 Try Ruby: 无需在你的系统中安装.Ruby,只要通过浏览器便可立即体验 Ruby ...

  7. 【转】获取手机的ipv4地址

    http://blog.csdn.net/yueqinglkong/article/details/17391051 直接贴代码: public class GetLocalIpAddress ext ...

  8. Segger RTT : Real Time Terminal SRAM 调试解决方法

    http://segger.com/jlink-real-time-terminal.html Real Time Terminal SEGGER's Real Time Terminal (RTT) ...

  9. hadoop备战:hadoop,hbase兼容版本号汇总

    Hbase的安装须要考虑Hadoop的版本号,即兼容性.有不足的希望能指出. 下面考究官网得到的,关于hadoop版本号和hbase版本号可到下面网址中下载:http://mirror.bit.edu ...

  10. Java带包编译运行

    package cn.togeek.job; public class Test { public static void main(String[] args) throws Exception { ...