• 【示例代码】 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 = 0;
                var random = new Random();
                for (int i = 0; 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 <= 1)
                    {
                        inCircle++;
                    }
                }
     
                return ((double)inCircle / iterations) * 4;
            }
        }
    }
    
    
    
    using System;
    using System.Diagnostics;
     
    namespace PiWithMonteCarlo.TestDriver
    {
        class Program
        {
            static void Main(string[] args)
            {
                var iterations = 20000000 * Environment.ProcessorCount;
     
                ExecuteAndPrint("Trivial PI Calculator", TrivialPiCalculator.Calculate, iterations);
                ExecuteAndPrint("
    (Stupid) Parallel.For PI Calculator", ParallelForPiCalculator.Calculate, iterations);
                ExecuteAndPrint("
    Parallel.For PI Calculator", EnhancedParallelForPiCalculator.Calculate, iterations);
                ExecuteAndPrint("
    PLinq PI Calculator", PlinqPiCalculator.Calculate, iterations);
                ExecuteAndPrint("
    Fast 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

  • 相关阅读:
    python3爬取咪咕音乐榜信息(附源代码)
    python3通过Beautif和XPath分别爬取“小猪短租-北京”租房信息,并对比时间效率(附源代码)
    在windows7 32ibt安装MongoDB数据库的方法及连接失败解决方案
    GoWeb开发_Iris框架讲解(一)
    Web项目开发介绍及实战项目介绍
    DAY7学习笔记
    DAY6课下作业
    DAY6学习笔记
    DAY5课下作业
    DAY5学习笔记
  • 原文地址:https://www.cnblogs.com/airven/p/5341929.html
Copyright © 2020-2023  润新知