Parallel Power并行的力量(一个测试代码)
2011-08-30 16:33
239 查看
Parallel using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
{
class Test
{
static void Main()
{
int max = 500;
var query = Enumerable.Range(0, max)
.Select(SlowProjection)
.Where(x => x > 10)
.AsParallel();
Stopwatch sw = Stopwatch.StartNew();
int count = query.Count();
sw.Stop();
Console.WriteLine("Count: {0} in {1}ms", count,
sw.ElapsedMilliseconds);
query = Enumerable.Range(0, max)
.AsParallel()
.Select(SlowProjection)
.Where(x => x > 10);
sw = Stopwatch.StartNew();
count = query.Count();
sw.Stop();
Console.WriteLine("Count: {0} in {1}ms", count,
sw.ElapsedMilliseconds);
Console.Read();
}
static int SlowProjection(int input)
{
Thread.Sleep(100);
return input;
}
}
}
这里比较了两种使用并行效率的比较,没有写顺序执行的原因你懂的.
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
{
class Test
{
static void Main()
{
int max = 500;
var query = Enumerable.Range(0, max)
.Select(SlowProjection)
.Where(x => x > 10)
.AsParallel();
Stopwatch sw = Stopwatch.StartNew();
int count = query.Count();
sw.Stop();
Console.WriteLine("Count: {0} in {1}ms", count,
sw.ElapsedMilliseconds);
query = Enumerable.Range(0, max)
.AsParallel()
.Select(SlowProjection)
.Where(x => x > 10);
sw = Stopwatch.StartNew();
count = query.Count();
sw.Stop();
Console.WriteLine("Count: {0} in {1}ms", count,
sw.ElapsedMilliseconds);
Console.Read();
}
static int SlowProjection(int input)
{
Thread.Sleep(100);
return input;
}
}
}
这里比较了两种使用并行效率的比较,没有写顺序执行的原因你懂的.
相关文章推荐
- 贴一个解决js异步并行等待的代码
- 20行代码写一个CSS覆盖率测试脚本
- 一个jdbc 测试程序代码
- c# Parallel.For 并行编程 执行顺序测试
- 一个新手为老代码写测试程序的心得
- python调用HTMLTestRunner+unittest实现一次执行多个测试类,并生成与每个测试类对应的测试报告,并不像某些人写的每次只执行一个测试类,具体看代码,附上整个project代码
- 面向对象_一个标准的手机的代码及测试
- 一个简单的动态编译器,支持C#和VB.NET。当你想测试一些简单的代码的时候可以使用。
- 测试python的并行模块Parallel Python的效率
- 最近的课设一个测试“敲击”的简单代码
- 一个BOOST 简单测试代码 for linux
- 代码大全学习-14-变量名的力量(Power of Variables Names)
- 使用Junit测试一个 spring静态工厂实例化bean 的例子,所有代码都没有问题,但是出现java.lang.IllegalArgumentException异常
- Supersonic关于一个Hash Join的测试代码分析
- 一个不错的TAB标签代码-经测试好用
- 测试Tomcat是否成功的一个测试代码(部分)
- 最近 因为 一个take阻塞 一直占有当前获取的lock 导致奇怪的现象 这里把自己的测试代码贴下 免得下次犯错
- Java代码中,一个类中的方法返回类型是该类本身的测试
- 改写了一个shell写的cdn节点测试代码