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>的更多相关文章

  1. Swift常用语法示例代码(二)

    此篇文章整理自我以前学习Swift时的一些练习代码,其存在的意义多是可以通过看示例代码更快地回忆Swift的主要语法. 如果你想系统学习Swift或者是Swift的初学者请绕路,感谢Github上Th ...

  2. Swift常用语法示例代码(一)

    此篇文章整理自我以前学习Swift时的一些练习代码,其存在的意义多是可以通过看示例代码更快地回忆Swift的主要语法. 如果你想系统学习Swift或者是Swift的初学者请绕路,感谢Github上Th ...

  3. java对接申通下单接口示例代码

    上面是控制台示例代码 public class Sample{ private final static String URL = "http://order.sto-express.cn: ...

  4. 浅谈C#中Tuple和Func的使用

    为什么将Tuple和Func混合起来谈呢? 首先,介绍一下:Tuple叫做元组,是.Net Framwork4.0引入的数据类型,用来返回多个数值.在C# 4.0之前我们函数有多个返回值,通常是使用r ...

  5. python golang中grpc 使用示例代码详解

    python 1.使用前准备,安装这三个库 pip install grpcio pip install protobuf pip install grpcio_tools 2.建立一个proto文件 ...

  6. 基于DotNetOpenAuth的OAuth实现示例代码: 获取access token

    1. 场景 根据OAuth 2.0规范,该场景发生于下面的流程图中的(D)(E)节点,根据已经得到的authorization code获取access token. 2. 实现环境 DotNetOp ...

  7. 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题

    调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...

  8. ActiveMQ笔记(1):编译、安装、示例代码

    一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...

  9. C#微信公众平台接入示例代码

    http://mp.weixin.qq.com/wiki/17/2d4265491f12608cd170a95559800f2d.html 这是微信公众平台提供的接入指南.官网只提供了php的示例代码 ...

  10. 编译opengl编程指南第八版示例代码通过

    最近在编译opengl编程指南第八版的示例代码,如下 #include <iostream> #include "vgl.h" #include "LoadS ...

随机推荐

  1. LeetCode——Unique Binary Search Trees II

    Question Given an integer n, generate all structurally unique BST's (binary search trees) that store ...

  2. HttpServlet实现serializable

    Java Servlet Technology Overview Servlets are the Java platform technology of choice for extending a ...

  3. CountDownLatch详解

    功能描述 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 常见用法 多个人等一个信号后继续执行操作.例如5个运动员,等一个发令员的枪响. 一个人等多个人的信号. ...

  4. 【性能测试】服务器性能监控、数据采集工具nmon安装使用详解

    nmon nmon是一种在AIX与各种Linux操作系统上广泛使用的监控与分析工具,它能在系统运行过程中实时地捕捉系统资源的使用情况,并且能输出结果到文件中,然后通过nmon_analyzer工具产生 ...

  5. php-fpm: 某项目网站频繁出现503问题解决( WARNING: [pool www] server reached pm.max_children setting (50), consider raising it)

    服务是nginx+php-fpm配置, 在运行过一段时间后,会经常出现: WARNING: [pool www] server reached pm.max_children setting (50) ...

  6. Canvas几种模式的区别

    1.screen space-overlay UI显示在最前方 2.screen space-camera 箭头指的是canvas 这样可以放置东西在UI前方和UI后方 3.world space 做 ...

  7. Swift 4.1 正式发布,新增更多泛型特性支持

    Swift 4.1 兼容 4.0,并做了一些改进,其中大部分通过了 Swift Evolution 流程.此次发布,包含了对核心语言的更新,包括新增更多对泛型的支持.新的构建选项,以及对 Swift ...

  8. Error:Cannot access first() element from an empty List

    解决方案: bintray版本问题,修改为: classpath 'com.novoda:bintray-release:0.3.4' 如下: buildscript { repositories { ...

  9. hihocoder1457

    http://hihocoder.com/problemset/problem/1457 找不重复子串的和 topo序搞一搞,用父亲更新儿子节点的val,记得乘上节点数 //#pragma comme ...

  10. 2-1 RHEL6.5 环境搭建与部署

    第二部分:Linux常见服务管理 2-1 RHEL6.5 环境搭建与部署 第二部分主要讲解的是开源服务搭建 学习方法与注意事项: 1. 端正态度,开始学习 2. 认真完成作业和实验(并详细记录) 3. ...