using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks; namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<int> aa = new List<int>();
Random Rand = new Random();
for (int i = ; i < ; i++)
{
aa.Add(Rand.Next());
}
Stopwatch st = new Stopwatch(); st.Start();
foreach (var item in aa)
{
item.ToString();
}
st.Stop();
Console.WriteLine("========" + st.ElapsedMilliseconds); st.Restart();
Parallel.ForEach(aa, (item, loopstate) => { item.ToString(); });
st.Stop();
Console.WriteLine("========" + st.ElapsedMilliseconds); st.Restart();
Parallel.ForEach(aa, new ParallelOptions() { MaxDegreeOfParallelism = },(item, loopstate) => { item.ToString(); }); //指定最大线程数
st.Stop();
Console.WriteLine("========" + st.ElapsedMilliseconds); st.Restart();
Parallel.ForEach(aa, (item, loopstate) => {
item.ToString();
if (item>)
{
loopstate.Stop();
}
if (loopstate.IsStopped)
{
Console.WriteLine("loopstate.IsStopped");
}
});
st.Stop();
Console.WriteLine("========" + st.ElapsedMilliseconds);
Console.Read();
}
}
}

并行forearch的使用及测试(Parallel.Foreach)的更多相关文章

  1. Parallel.ForEach() 并行循环

    现在的电脑几乎都是多核的,但在软件中并还没有跟上这个节奏,大多数软件还是采用传统的方式,并没有很好的发挥多核的优势. 微软的并行运算平台(Microsoft’s Parallel Computing ...

  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. Parallel.Foreach

    随着多核时代的到来,并行开发越来越展示出它的强大威力! 使用并行程序,充分的利用系统资源,提高程序的性能.在.net 4.0中,微软给我们提供了一个新的命名空间:System.Threading.Ta ...

  5. C# 多线程 Parallel.For 和 For 谁的效率高?那么 Parallel.ForEach 和 ForEach 呢?

    还是那句话:十年河东,十年河西,莫欺少年穷. 今天和大家探讨一个问题:Parallel.For 和 For 谁的效率高呢? 从CPU使用方面而言,Parallel.For 属于多线程范畴,可以开辟多个 ...

  6. Parallel.Foreach的基础知识

    微软的并行运算平台(Microsoft’s Parallel Computing Platform (PCP))提供了这样一个工具,让软件开发人员可以有效的使用多核提供的性能. Visual Stud ...

  7. Parallel.ForEach 之 MaxDegreeOfParallelism

    参考:Max Degree of Parallelism最大并行度配置 结论: 与设置的线程数有关 有设置的并行度有关 测试如下: @@@code System.Threading.ThreadPoo ...

  8. Parallel.ForEach , ThreadPool.QueueUserWorkItem

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

  9. Parallel.ForEach 多线程 声明失败 "未将对象引用设置到对象的实例"

    x using System; using System.Collections.Generic; namespace Parallel.ForEach { class Program { //代码结 ...

随机推荐

  1. NetworkX系列教程(1)-创建graph

    小书匠Graph图论 研究中经常涉及到图论的相关知识,而且常常面对某些术语时,根本不知道在说什么.前不久接触了NetworkX这个graph处理工具,发现这个工具已经解决绝大部分的图论问题(也许只是我 ...

  2. properties的编码问题

    1.在eclipse中,如果不专门设置,properties文件的编码是ISO-8859-1,最好将其改为UTF-8 2.当properties文件的编码改为UTF-8还不够,Spring的conte ...

  3. mac 安装 mysql 5.7

    下载 https://dev.mysql.com/downloads/mysql/5.7.html#downloads 下一步,经过一系列安装步骤后,会跳出一个这样的界面,请注意!!! 上面红框中是你 ...

  4. 走进JavaWeb技术世界8:浅析Tomcat9请求处理流程与启动部署过程

    谈谈 Tomcat 请求处理流程 转自:https://github.com/c-rainstorm/blog/blob/tomcat-request-process/reading-notes &l ...

  5. 异步任务神器 和定时任务Celery

    异步任务神器 Celery Celery 在程序的运行过程中,我们经常会碰到一些耗时耗资源的操作,为了避免它们阻塞主程序的运行,我们经常会采用多线程或异步任务.比如,在 Web 开发中,对新用户的注册 ...

  6. Linux 安装软件报错 Sub-process /usr/bin/dpkg returned an error code (1)

    Linux 通过 apt-get 安装软件时报错,换一个软件安装也一样. Errors were encountered while processing: blueman E: Sub-proces ...

  7. git 删除目录及子目录下的同名文件

    find . -name ".git" | xargs rm -Rf find . -name ".gitignore" | xargs rm -Rf

  8. 安装openssh-server报Depends: openssh-client (= 1:6.6p1-2ubuntu2.8)错误

    SFTP称作“安全的FTP”,它使用ssh文件传输协议.所以我们需要安装openssh-server ubuntu自带的有openssh-client,所以可以通过 ssh username@host ...

  9. Kafka、RabbitMQ、RocketMQ、ActiveMQ消息中间件的对比--多年生产经验实践总结

    引言 分布式系统中,我们广泛运用消息中间件进行系统间的数据交换,便于异步解耦.现在开源的消息中间件有很多,前段时间我们自家的产品 RocketMQ (MetaQ的内核) 也顺利开源,得到大家的关注. ...

  10. 简易的CRM系统案例SpringBoot + thymeleaf + MySQL + MyBatis版本

    创建maven项目 pop.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns ...