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. NodeJS基础知识

    console.log方法: console.log("This is a test string"); node app.js 使用重定向标准输出流: node app.js 1 ...

  2. Pytest从测试类外为测试用例动态注入数据

    今天Nelly问我Pytest能不能支持从TestClass类外传入参数?从类外批量传入各个test方法需要的参数.因为数据文件可能有很多情况,不方便依次匹配. 然而又必须用类对用例进行归类及复用,数 ...

  3. 使用python2 对服务器监控(监控内存,CPU,网卡流量等)

    #!/usr/bin/env python # -*- coding:utf- -*- #create at -- 'this is a system monitor scripts' __autho ...

  4. elasticsearch启动错误

    requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled java.lang.UnsupportedOpe ...

  5. C#读写三菱PLC数据 使用TCP/IP 协议

    本文将使用一个Github开源的组件库技术来读写三菱PLC和西门子plc数据,使用的是基于以太网的TCP/IP实现,不需要额外的组件,读取操作只要放到后台线程就不会卡死线程,本组件支持超级方便的高性能 ...

  6. 【hadoop环境问题】namenode无法启动问题解决

    [问题背景] 要在自己的阿里云上搭伪分布式,用来复习和强化hive相关的知识,在执行脚本 sh start-dfs.sh后,jps后: 然后查看name的日志,报错如下: [解决方法] 网上的很多方法 ...

  7. OpenResty之replace-filter-nginx-module

    原文: openresty/replace-filter-nginx-module 1. 概要 location /t { default_type text/html; echo abc; repl ...

  8. 快速安装python3

    使用 rpm 包进行安装 先来介绍一下 IUS 这个社区,名字的全写是[Inline with Upstream Stable]取首字母,它主要是一个提供新版本RPM包的社区.具体使用可以查看官方文档 ...

  9. PHP 美化输出数组

    var_export — 输出或返回一个变量的字符串表示 此函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码. 您可以通过将函数的 ...

  10. 【转载】 AutoML总结

    原文地址: https://jinxin0924.github.io/2017/12/21/AutoML%E6%80%BB%E7%BB%93/ Posted by JxKing on December ...