zoukankan      html  css  js  c++  java
  • 【示例代码】 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

  • 相关阅读:
    左偏树
    论在Windows下远程连接Ubuntu
    ZOJ 3711 Give Me Your Hand
    SGU 495. Kids and Prizes
    POJ 2151 Check the difficulty of problems
    CodeForces 148D. Bag of mice
    HDU 3631 Shortest Path
    HDU 1869 六度分离
    HDU 2544 最短路
    HDU 3584 Cube
  • 原文地址:https://www.cnblogs.com/airven/p/5341929.html
Copyright © 2011-2022 走看看