【示例代码】 Tuple<T> Func<T>
using System;
using System.Math; namespace PiWithMonteCarlo
{
/// <summary>
/// Trivial, synchronous calculation algorithm
/// </summary>
public static class TrivialPiCalculator
{
public static double Calculate(int iterations)
{
int inCircle = ;
var random = new Random();
for (int i = ; i < iterations; i++)
{
var a = random.NextDouble();
var b = random.NextDouble(); // Strictly speaking, we do not need Sqrt here. We could simply drop it and still get the
// same result. However, this sample should demonstrate some perf topics, too. Therefore
// it stays there just so the program has to do some math.
#if LANG_EXPERIMENTAL
var c = Sqrt(a * a + b * b);
#else
var c = Math.Sqrt(a * a + b * b);
#endif
if (c <= )
{
inCircle++;
}
} return ((double)inCircle / iterations) * ;
}
}
} using System;
using System.Diagnostics; namespace PiWithMonteCarlo.TestDriver
{
class Program
{
static void Main(string[] args)
{
var iterations = * Environment.ProcessorCount; ExecuteAndPrint("Trivial PI Calculator", TrivialPiCalculator.Calculate, iterations);
ExecuteAndPrint("\n(Stupid) Parallel.For PI Calculator", ParallelForPiCalculator.Calculate, iterations);
ExecuteAndPrint("\nParallel.For PI Calculator", EnhancedParallelForPiCalculator.Calculate, iterations);
ExecuteAndPrint("\nPLinq PI Calculator", PlinqPiCalculator.Calculate, iterations);
ExecuteAndPrint("\nFast PI Calculator", FastPiCalculator.Calculate, iterations);
} private static void ExecuteAndPrint(string label, Func<int, double> calculation, int iterations)
{
Console.WriteLine(label);
PrintResult(Measure(() => calculation(iterations)), iterations);
} private static void PrintResult(Tuple<double, TimeSpan> r, int iterations)
{
Console.WriteLine(
"{0} ({1:#,##0.0000} sec for {2:#,##0} iterations = {3:#,##0.00} iter/sec)",
r.Item1,
r.Item2.TotalSeconds,
iterations,
iterations / r.Item2.TotalSeconds);
} private static Tuple<T, TimeSpan> Measure<T>(Func<T> body)
{
var watch = new Stopwatch();
watch.Start();
var result = body();
watch.Stop();
return new Tuple<T, TimeSpan>(result, watch.Elapsed);
}
}
}
更多地址
http://www.software-architects.com/devblog/2014/09/22/C-Parallel-and-Async-Programming
【示例代码】 Tuple<T> Func<T>的更多相关文章
- Swift常用语法示例代码(二)
此篇文章整理自我以前学习Swift时的一些练习代码,其存在的意义多是可以通过看示例代码更快地回忆Swift的主要语法. 如果你想系统学习Swift或者是Swift的初学者请绕路,感谢Github上Th ...
- Swift常用语法示例代码(一)
此篇文章整理自我以前学习Swift时的一些练习代码,其存在的意义多是可以通过看示例代码更快地回忆Swift的主要语法. 如果你想系统学习Swift或者是Swift的初学者请绕路,感谢Github上Th ...
- java对接申通下单接口示例代码
上面是控制台示例代码 public class Sample{ private final static String URL = "http://order.sto-express.cn: ...
- 浅谈C#中Tuple和Func的使用
为什么将Tuple和Func混合起来谈呢? 首先,介绍一下:Tuple叫做元组,是.Net Framwork4.0引入的数据类型,用来返回多个数值.在C# 4.0之前我们函数有多个返回值,通常是使用r ...
- python golang中grpc 使用示例代码详解
python 1.使用前准备,安装这三个库 pip install grpcio pip install protobuf pip install grpcio_tools 2.建立一个proto文件 ...
- 基于DotNetOpenAuth的OAuth实现示例代码: 获取access token
1. 场景 根据OAuth 2.0规范,该场景发生于下面的流程图中的(D)(E)节点,根据已经得到的authorization code获取access token. 2. 实现环境 DotNetOp ...
- 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题
调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...
- ActiveMQ笔记(1):编译、安装、示例代码
一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...
- C#微信公众平台接入示例代码
http://mp.weixin.qq.com/wiki/17/2d4265491f12608cd170a95559800f2d.html 这是微信公众平台提供的接入指南.官网只提供了php的示例代码 ...
- 编译opengl编程指南第八版示例代码通过
最近在编译opengl编程指南第八版的示例代码,如下 #include <iostream> #include "vgl.h" #include "LoadS ...
随机推荐
- 搭建Nginx图片服务器(Linux)
知识点: 在Linux系统上安装Nginx服务器,配置图片访问路径 通过ftp上传图片到,指定路径,通过浏览器访问指定路径中的图片 参考博客:http://blog.csdn.net/maoyuanm ...
- 关于Vue的component制作dialog组件
其实原理很简单,兴个粟子, 点击按钮出现 dialog 弹出杠, 将dialog做成一个组件,components/dialog.vue 就是在components里面新建一个vue.将这个vue做为 ...
- linux du命令的疑惑
起因是测试rsync传输数据.传输完成后,想看一下传输的文件是不是完整,所以检测了下源目录和目标目录的大小,竟然出现了巨大的差距: [root@w anaconda3]$ du -sh ./ .9G ...
- linux基础之Vim
- spark 性能优化
1.内存 spark.storage.memoryFraction:很明显,是指spark缓存的大小,默认比例0.6 spark.shuffle.memoryFraction:管理executor中R ...
- 值得推荐的10本PHP书籍(转)
值得推荐的10本PHP书籍(转) 一.总结 一句话总结: 二.值得推荐的10本PHP书籍 本篇文章的目的是想较全面地推荐10本PHP书籍,暂不讨论Linux/NGINX/Mysql等其他丛书. 前言 ...
- CSS3:@font-face规则
前言 过去,Web设计师为了保证网站能够正常显示,只能使用“Web安全字体”,即每台机器都预装的字体.但Web安全字体有时并不好看... @font-face能够使得任何一台机器能够显示理想中的字体. ...
- 【转】c++析构函数(Destructor)
创建对象时系统会自动调用构造函数进行初始化工作,同样,销毁对象时系统也会自动调用一个函数来进行清理工作,例如释放分配的内存.关闭打开的文件等,这个函数就是析构函数. 析构函数(Destructor)也 ...
- es6 nodejs compose
const compose = (...fns) => { let len = fns.length; let fn_index = len - 1; let fn_result; functi ...
- Linux命令 ls -l 输出内容含义详解
Linux命令 ls -l s输出内容含义详解 1. ls 只显示文件名或者文件目录 2. ls -l(这个参数是字母L的小写,不是数字1) 用来查看详细的文件资料 在某个目录下键入ls -l可 ...