一个并行程序
来源:互联网 发布:php实战视频教程 编辑:程序博客网 时间:2024/06/01 17:06
using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace GetPrimes{ internal class ProcessDataInParallel { private static void Main(string[] args) { int maxPrimes = 1000000; int maxNumber = 20000000; long primesFound = 0; Console.WriteLine("Iterative"); Stopwatch watch = new Stopwatch(); watch.Start(); for (UInt32 i = 0; i < maxNumber; ++i) { if (IsPrime(i)) { Interlocked.Increment(ref primesFound); if (primesFound > maxPrimes) { Console.WriteLine("Last prime found:{0:N0}", i); break; } } } watch.Stop(); Console.WriteLine("Found {0:N0} primes in {1}", primesFound, watch.Elapsed); watch.Reset(); primesFound = 0; Console.WriteLine("Parallel"); watch.Start(); //in order to stop the loop, there is an //overload that takes Action<int, ParallelLoopState> Parallel.For(0, maxNumber, (i, loopState) => { if (IsPrime((UInt32)i)) { Interlocked.Increment(ref primesFound); if (primesFound > maxPrimes) { Console.WriteLine("Last prime found:{0:N0}", i); loopState.Stop(); } } }); watch.Stop(); Console.WriteLine("Found {0:N0} primes in {1}", primesFound, watch.Elapsed); Console.ReadKey(); } public static bool IsPrime(UInt32 number) { //check for evenness if (number % 2 == 0) { if (number == 2) { return true; } return false; } //don’¡¥t need to check past the square root UInt32 max = (UInt32)Math.Sqrt(number); for (UInt32 i = 3; i <= max; i += 2) { if ((number % i) == 0) { return false; } } return true; } }}
运行结果:
- 一个并行程序
- 一个有趣的并行程序
- 利用PS3和Linux搭建一个并行程序开发平台
- Qt OpenMP并行程序
- 测试并行程序
- 测试并行程序
- 测试并行程序
- 并行程序“锁”相关
- SPLASH2并行测试程序
- R并行程序框架
- matlab 程序 并行执行
- 串行程序并行化
- Java并行程序基础
- Java并行程序基础
- java并行程序基础
- Java 并行程序基础
- 一个并行搜索算法
- 分享一个基于winForm后台单线程推送多线程并行处理业务的小程序框架
- sqlserver 差异备份与还原示例
- cocosbuilder
- sql cast()函数
- java基础:线程
- Android多媒体学习:检索MediaStore中的Video和其对应的缩略图信息
- 一个并行程序
- mybatis学习笔记---入门
- 延迟任务和周期任务的实现(ScheduledThreadPoolExecutor)
- MTP 与MSC 在FS上修改点
- 与监控有关的开源项目汇总
- ifconfig、ifup、ifdown
- 如何开机自启动程序
- iBatis导读 ibaitis 级联查询
- 松开手,你可以拥有更多