TPL
namespace TPLTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//var list = testFillParallel();
//int i = list.Count(); Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = ; i < ; i++)
{
ProcessLongTime(i);
}
//watch.Stop();
this.label1.Text = string.Format("顺序执行用时:" + watch.ElapsedMilliseconds); watch.Restart();
//watch.Start();
Parallel.For(, , p =>
{
ProcessLongTime(p);
//MessageBox.Show(p.ToString());
});
//watch.Stop();
this.label2.Text = string.Format("并行执行用时:" + watch.ElapsedMilliseconds); //watch.Restart();
////watch.Start();
//for (int i = 1; i <= 5; i++)
//{
// Task myTask = new Task(obj => ProcessLongTime((int)obj), i);
// myTask.Start();
//}
////watch.Stop();
//this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds); watch.Restart();
int count = ;
int results;
Semaphore semaphore = new Semaphore(, count);
System.Threading.Tasks.Parallel.For(, count, i =>
{
results = ProcessLongTimeI(i);
semaphore.Release();
}); //for (var i = 0; i <= count; i++)
//{
// semaphore.WaitOne();
// //Console.WriteLine("Got " + i);
//}
watch.Stop();
this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds);
} private IEnumerable<Person> testFillParallel()
{
//var list = new List<Person>(9);
var list = new BlockingCollection<Person>(); //必须使用线程安全的集合类型 Enumerable.Range(, ).AsParallel().ForAll(n =>
{
var name = "Person" + n%;
if (list.Count(p => p.Name == name) < ) list.Add(new Person {Id = n, Name = name});
});
this.label1.Text=string.Format("Person's count is {0}", list.Count);
return list;
} private void ProcessLongTime(int mi)
{
for (int i = ; i < ; i++)
{
i++;
i--;
}
} private int ProcessLongTimeI(int mi)
{
for (int i = ; i < ; i++)
{
i++;
i--;
}
return mi;
} private void button2_Click(object sender, EventArgs e)
{
List<int> ls=new List<int>();
for (int i = ; i < ; i++)
{
ls.Add(i);
} Stopwatch watch = new Stopwatch();
watch.Start();
foreach (int i in ls)
{
ProcessLongTime(i);
}
this.label1.Text = string.Format("顺序执行用时:" + watch.ElapsedMilliseconds); watch.Restart();
Parallel.ForEach(ls,p=>
{
ProcessLongTime(p);
//MessageBox.Show(p.ToString());
});
this.label2.Text = string.Format("并行执行用时:" + watch.ElapsedMilliseconds); watch.Restart();
int count = ;
int results;
Semaphore semaphore = new Semaphore(, ls.Count);
System.Threading.Tasks.Parallel.ForEach(ls, p =>
{
results = ProcessLongTimeI(p);
semaphore.Release();
}); watch.Stop();
this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds);
}
}
TPL的更多相关文章
- 今天再给大家带点html5前端开发的干货模板“text/tpl”怎么用 script template怎么用
text/tpl 顾名思义就是模板,其实和C++模板函数类似的作用,就是利用他生成一个HMTL内容,然后append或者替换html里面 有什么好处,假如后端返回来的数据都是一样的,但是需要生成不同的 ...
- 发布一个开源极致的javascript模板引擎tpl.js
tpl.js(大家直接去https://git.oschina.net/tianqiq/tpl.js这个上面看) 简介 tpl.js是一个比较极致(极小,极快,极简单)的js模板引擎,可以在各种js环 ...
- 如何在tpl模版的div块中加ztree
ld-ztree.tpl <div class="ld-ztree-container"> <div class="ld-ztree-header te ...
- TPL实现Task.WhileAll扩展方法
文章翻译整理自 Nikola Malovic 两篇博文: Task.WhileAll Awaitable task progress reporting 当 Task.WhenAll 遇见 Task. ...
- tpl + ccr
不是非此即彼的场景.如下混合使用CCR+TPL的代码说明问题:It's not an either/or scenario.You can intermix CCR and TPL code like ...
- C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是TAP(Task-based Asynchronous Pattern, 基于任务的异步模式)
学习书籍: <C#本质论> 1--C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是 ...
- Using the Task Parallel Library (TPL) for Events
Using the Task Parallel Library (TPL) for Events The parallel tasks library was introduced with the ...
- TPL(Task Parallel Library)多线程、并发功能
The Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and System ...
- 一个使用C#的TPL Dataflow Library的例子:分析文本文件中词频
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:一个使用C#的TPL Dataflow Library的例子:分析文本文件中词频.
- /Home/Tpl/Equipment/rangeIndex.html 里调用魔板
<pre name="code" class="html">demo:/var/www/DEVOPS# vim ./Home/Tpl/Equipme ...
随机推荐
- 商品库存SKU
一种解决方案(性能垃圾,基本实现功能)商品表 属性集表 属性表 属性值表 SKU表 SKU选项表 属性集和属性之间的中间表表关系商品表 *--------------1 属性集表属性集表 *- ...
- selenium + python网页自动化测试环境搭建
1.python的安装 ,这个不解释,exe文件运行安装即可,既然你选择python,相信你是熟悉python的,我安装目录C:\Python27 2.setuptools 的安装也非常简单,同样是e ...
- 为什么dubbo使用ZkClient作为zookeeper的客户端
本文内容并非原创,使用资料均来自互联网. dubbo使用了zkClient而不是使用zookeeper本身的客户端与zookeeper进行交互,为什么呢? 先看看zookeeper本身自带的客户端的问 ...
- UINavigationController 总结
一 . UINavigationBar 1.获取 UINavigationBar 对象: [UINavigationBar appearance] ,可以通过该方法对全部 navigation 进行设 ...
- 4.0 spring-注册解析的Bean
1.0 registerBeanDefinition 对于配置文件,解析也解析完了,装饰也装饰完了,对于得到的BeanDefinition已经可以满足后续的使用了,唯一剩下的工作就是注册了, 也就是: ...
- 【leetcode】Permutations II (middle)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- GIT:本地有更改,但强制作远程仓库里作更新
有时,紧急线上修改时,这个功能有用处的. git fetch --all git reset --hard origin/master ================ git reset --har ...
- 李洪强iOS开发之OC[018]对象和方法之间的关系
// // main.m // 18 - 对象和方法之间的关系 // // Created by vic fan on 16/7/14. // Copyright © 2016年 李洪强. A ...
- MyEclipse中创建maven工程
转载:http://blog.sina.com.cn/s/blog_4f925fc30102epdv.html 先要在MyEclipse中对Maven进行设置: 到此Maven对MyEclip ...
- boost库在windows下的编译和使用
因为跨平台的原因,现在要使用到boost库,boost库非常大,现在处于摸索阶段. 首先来说boost库在window下的安装和使用. 一.下载 首先从boost官方主页http://www.boos ...