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 ...
随机推荐
- resin access.log format配置详解
The access log formatting variables follow the Apache variables: %b result content length %D tim ...
- bnuoj 27987 Record of the Attack at the Orbit (模拟)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=27987 [题意]:给定坐标输出图形 [题解]:处理坐标上的小技巧 [code]: #include ...
- springmvc 数据对象回绑
springmvc中,由页面 post到 controller,对象可以在form里面设置modelAttribute达到回绑的目的. 但是如果对象里面有复杂的非String,int的对象,则要在co ...
- PAT-乙级-1010. 一元多项式求导 (25)
1010. 一元多项式求导 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 设计函数求一元多项式的导数.(注:xn(n为整数)的一 ...
- 用printf做彩色日志记录
写了一个简单的程序,但是考虑到有一些信息是需要打印在控制台上的,就像在windows上启动apache tomcat时控制台显示的信息一样.琢磨一会儿之后,对printf进行了封装,支持控制台打印日志 ...
- ajax请求返回json数据弹出下载框的解决方法
将返回的Content-Type由application/json改为text/html. 在struts2下: <action name="XXXAjax" class=& ...
- Maven Source jar
http://blog.csdn.net/symgdwyh/article/details/4407945
- FreeMarker 乱码解决方案 生成静态html文件
读取模板的时候有一个编码: Template template = this.tempConfiguration.getTemplate(templatePath,"UTF-8") ...
- UIActinSheet和UIActionSheetDelegate
UIActinSheet和UIActionSheetDelegate 这个是就那个UIActionSheet对象 一般用来选择类型或者改变界面...还有更多应用 定义如下:UIActionSheet ...
- 李洪强iOS开发之【零基础学习iOS开发】【02-C语言】08-基本运算
计算机的基本能力就是计算,所以一门程序设计语言的计算能力是非常重要的.C语言之所以无所不能,是因为它不仅有丰富的数据类型,还有强大的计算能力.C语言一共有34种运算符,包括了常见的加减乘除运算.这讲就 ...