Exemplo n.º 1
0
        public static void TestStdDev()
        {
            //List<int> integers = new List<int> { 3, 4, 5, 6, 7 };
            List <double> doubles = new List <double> {
                3.0, 4.0, 5.0, 6.0, 7.0
            };
            double doubleStdDev = StatisticsHelper.StdDev(doubles);

            Console.WriteLine(doubleStdDev);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 1: Using While ViewHelper.AskForDoubleWhile <br/>
        /// 2: Do-While loop AskForDouble <br/>
        /// 3: No clear arg: AskForDoubleNoClearArg <br/>
        /// 4: AskForDoubleNoClearArgBreakLoop ->
        /// 5: AskForDoubleNoClearArgBreakLoopWriteOptimised
        /// </summary>
        private static void ViewHelperTimeImprovement()
        {
            Dictionary <string, List <double> > triesData = new Dictionary <string, List <double> >
            {
                { "UsingWhile", new List <double>() },
                { "UsingDoWhile", new List <double>() },
                { "ViewHelper as an object", new List <double>() },
                { "BreakLoop", new List <double>() },
                { "tryparse", new List <double>() },
                { "WriteFormatting", new List <double>() }
            };
            List <ViewHelperData> viewHelperData = new List <ViewHelperData>();
            Stopwatch             watch          = new Stopwatch();
            ViewHelperData        vhdata;
            var vhconst = new ViewHelperConstructor();

            watch.Start();
            while (watch.ElapsedMilliseconds < 1200)  // A Warmup of 1000-1500 mS
            {                                         // stabilizes the CPU cache and pipeline.
                ViewHelper.AskForDoubleWhile("WARM UP");
            }
            watch.Stop();
            watch.Reset();

            for (int i = 0; i < 2000; i++)
            {
                //Mean: 0,5192 Median: 0,37765
                vhdata = new ViewHelperData();
                watch.Start();
                ViewHelper.AskForDoubleWhile("while");
                watch.Stop();
                triesData["UsingWhile"].Add(watch.ElapsedTicks);
                vhdata.UsingWhile = watch.ElapsedTicks;
                watch.Reset();

                //Mean: 0,4811 Median: 0,3746
                watch.Start();
                ViewHelper.AskForDouble("default");
                watch.Stop();
                triesData["UsingDoWhile"].Add(watch.ElapsedTicks);
                vhdata.UsingDoWhile = watch.ElapsedTicks;
                watch.Reset();

                //Mean: 0,4813 Median: 0,37515
                watch.Start();
                vhconst.AskForDouble("vhconst hello");
                watch.Stop();
                triesData["ViewHelper as an object"].Add(watch.ElapsedTicks);
                vhdata.ViewHelperAsAnObject = watch.ElapsedTicks;
                watch.Reset();

                //Mean: 0,4360 Median: 0,3569
                watch.Start();
                ViewHelper.AskForDoubleBreakLoop("break loop!");
                watch.Stop();
                triesData["BreakLoop"].Add(watch.ElapsedTicks);
                vhdata.BreakLoop = watch.ElapsedTicks;
                watch.Reset();

                // What happened?
                //Mean: 0,1703 Median: 0,0531
                watch.Start();
                ViewHelper.AskForDoubleTryParse("tryparse");
                watch.Stop();
                triesData["tryparse"].Add(watch.ElapsedTicks);
                vhdata.TryParse = watch.ElapsedTicks;
                watch.Reset();

                //Mean: 0,4446 Median: 0,3784
                watch.Start();
                ViewHelper.AskForDoubleOptimised("write optimised");
                watch.Stop();
                triesData["WriteFormatting"].Add(watch.ElapsedTicks);
                vhdata.WriteOptimised = watch.ElapsedTicks;
                watch.Reset();

                viewHelperData.Add(vhdata);
            }
            Console.WriteLine();
            string outputFormat = "{0,-20} Min: {1,-9} Mean: {2,-9} Median: {3,-9} StDev: {4,-9} Max: {5,-9}";

            foreach (var element in triesData)
            {
                Console.WriteLine(outputFormat, element.Key, element.Value.Min(), element.Value.Average(),
                                  StatisticsHelper.GetMedian(element.Value), StatisticsHelper.StdDev(element.Value), element.Value.Max());
            }
            //Console.WriteLine("Allocated memory: {0}kb", GC.GetTotalAllocatedBytes(true)/1024);
            //string filename = $"viewHelperImprovement_{DateTime.Now.Millisecond}.csv";
            //using (var writer = new StreamWriter(filename))
            //using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
            //{
            //    csv.WriteRecords(viewHelperData);
            //}
            //Console.WriteLine("Data saved to: " + filename);
        }