现在的电脑几乎都是多核的,但在软件中并还没有跟上这个节奏,大多数软件还是采用传统的方式,并没有很好的发挥多核的优势。
微软的并行运算平台(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. c语言中用宏定义一个常量,数字后面带个U, L, F的含义

    转: c语言中数字后面带个U是什么意思?#define F_CPU 12000000U答:U表示该常数用无符号整型方式存储,相当于unsigned int;L表示该常数用长整型方式存储,相当于long ...

  2. HIT 2275 Number sequence

    点击打开HIT 2275 思路: 树状数组 分析: 1 题目要求的是总共的搭配方式,满足Ai < Aj > Ak.并且i j k不同 2 我们开两个树状数组,第一个在输入的时候就去更新.然 ...

  3. HDU 4035Maze(概率DP)

    HDU 4035   Maze 体会到了状态转移,化简方程的重要性 题解转自http://blog.csdn.net/morgan_xww/article/details/6776947 /** dp ...

  4. Android导入自定义的jar包时出现 E/AndroidRuntime(486): java.lang.NoClassDefFoundError错误

    把自定义的jar包放在Android的工程的libs目录下,运行程序,会出现一下错误: 10-10 08:34:06.479: E/dalvikvm(486): Could not find clas ...

  5. jQuery分别获取选中的复选框值

    function jqchk(){  //jquery获取复选框值   var s='';   $('input[name="aihao"]:checked').each(func ...

  6. ajax跳转页面问题

    $.ajax({ type:"POST", url: //你的请求程序页面随便啦 async:false,//同步:意思是当有返回值以后才会进行后面的js程序. data://请求 ...

  7. jQuery操作checkbox选择

    1.checkbox list选择 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  8. Codeforces Beta Round #18 (Div. 2 Only) C. Stripe 前缀和

    C. Stripe Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/18/C ...

  9. ExtJS grid tableGrid study

    Q:  How to color the text in the grid Try: http://dev.sencha.com/playpen/docs/output/Ext.grid.TableG ...

  10. phpmyadmi 上传大文件

    最近自己想通过phpmyadmin上传大的文件,找了很多方法都没搞定,今天特意静下心来,终于搞定了. 需求:将480M大小的sql文件通过phpmyadmin进行导入 1.首先修改php.ini文件, ...