예제 #1
0
파일: Program.cs 프로젝트: Erhannis/Jibu
        static void Main(string[] args)
        {
            Console.WriteLine("Pi is: 3.14159265358979323846... according to Wikipedia");



            // how many 'darts' do we throw at the circle/square
            int darts = 1000000000;

            // Let the JIT compilation kick in.
            MonteCarloPi.Estimate(10000000);
            MonteCarloPi.ParallelEstimate(10000000);

            Console.WriteLine("Calculating pi using 1 core");
            long   start     = DateTime.Now.Ticks;
            double seqResult = MonteCarloPi.Estimate(darts, 1);
            long   t1        = DateTime.Now.Ticks - start;

            Console.WriteLine(seqResult + " calculated in " + t1 / 1E4 + " ms");

            Console.WriteLine("Calculating pi using " + Manager.ProcessorCount + " core(s)");
            start = DateTime.Now.Ticks;
            double parResult = MonteCarloPi.ParallelEstimate(darts);
            long   t2        = DateTime.Now.Ticks - start;

            Console.WriteLine(parResult + " calculated in " + t2 / 1E4 + " ms");

            Console.WriteLine("Speed up: " + (double)t1 / t2);

            Console.Read();
        }
예제 #2
0
파일: Program.cs 프로젝트: Erhannis/Jibu
            public override Double Run()
            {
                // We use the reduce method to distribute work to 'procCount' tasks
                // and combine the results.
                // The grainSize parameter is 1 => one task for each iteration.
                double sum = Parallel.Reduce <double>(0, procCount, 0.0, 1,
                                                      delegate(int i) { return(MonteCarloPi.Estimate(iterations, i)); },
                                                      delegate(double op1, double op2) { return(op1 + op2); });

                return(sum / procCount);
            }
        public void IsPointInCircle_ReturnsTrueOrFalse(decimal x, decimal y, bool expectedInCircle)
        {
            var point = new MonteCarloPi.Point {
                X = x, Y = y
            };
            var monteCarloPi = new MonteCarloPi();

            var actualIsPointInCircle = monteCarloPi.IsPointInCircle(point);

            actualIsPointInCircle.Should().Be(expectedInCircle);
        }
        public void GetRandomPoint_ReturnsPoint()
        {
            var randomPoint = new MonteCarloPi().GetRandomPoint();

            randomPoint.Should().NotBeNull();
        }
        public void CalculatePi()
        {
            var pi = new MonteCarloPi().CalculatePi(100000);

            _testOutputHelper.WriteLine("pi: " + pi.ToString("0.000"));
        }