private void PrintData(AlgStats stats)
        {
            var actualDoubleFormatted = stats.ActualDoublingRatio < 0
                ? "na".PadLeft(20)
                : stats.ActualDoublingRatio.ToString("F2").PadLeft(20);
            var expectDoubleFormatted = stats.ExpectedDoublingRatio < 0
                ? "na".PadLeft(16)
                : stats.ExpectedDoublingRatio.ToString("F2").PadLeft(16);

            Console.Write(
                $"{stats.TimeMicro,20:F2} {actualDoubleFormatted} {expectDoubleFormatted} {stats.AlgResult,30:N0} {Math.Ceiling(Math.Log2(stats.n)),12:N0}");
        }
Пример #2
0
 public void FibLoopDoublingCalc(AlgStats algStats)
 {
     // Only calculate doubling ratios for even values of n and if n/2 exists in prev times table.
     if (algStats.n % 2 == 0 && algStats.PrevTimesTable.TryGetValue(algStats.n / 2, out double halfTime))
     {
         algStats.ActualDoublingRatio   = (algStats.TimeMicro / halfTime);
         algStats.ExpectedDoublingRatio = algStats.n / ((double)algStats.n / 2);
     }
     else
     {
         algStats.ActualDoublingRatio   = -1;
         algStats.ExpectedDoublingRatio = -1;
     }
 }
Пример #3
0
 public void FibMatrixDoublingCalc(AlgStats algStats)
 {
     // Only calculate doubling ratios for even values of n and if n/2 exists in prev times table. n must be > 2 for log, otherwise div by 0.
     if (algStats.n > 2 && algStats.n % 2 == 0 && algStats.PrevTimesTable.TryGetValue(algStats.n / 2, out double halfTime))
     {
         algStats.ActualDoublingRatio   = (algStats.TimeMicro / halfTime);
         algStats.ExpectedDoublingRatio = Math.Log2(algStats.n) / Math.Log2(((double)algStats.n / 2));
     }
     else
     {
         algStats.ActualDoublingRatio   = -1;
         algStats.ExpectedDoublingRatio = -1;
     }
 }
Пример #4
0
        public void FibRecurDoublingCalc(AlgStats algStats)
        {
            // Only calculate doubling ratios for even values of n and if n/2 exists in prev times table.
            if (algStats.n % 2 == 0 && algStats.PrevTimesTable.TryGetValue(algStats.n / 2, out double halfTime))
            {
                algStats.ActualDoublingRatio = (algStats.TimeMicro / halfTime);

                double goldenRatio = 1.61803398875;
                algStats.ExpectedDoublingRatio = Math.Pow(goldenRatio, algStats.n) / Math.Pow(goldenRatio, ((double)algStats.n / 2));
            }
            else
            {
                algStats.ActualDoublingRatio   = -1;
                algStats.ExpectedDoublingRatio = -1;
            }
        }
        private void AlgorithmTestRuntime(Algorithm algorithm, DoublingCalculator doublingCalc)
        {
            PrintHeader(algorithm);

            var currentStats = new AlgStats();

            for (var n = NMin; n < NMax; n++)
            {
                currentStats.n = n;
                if (currentStats.TimeMicro > MaxMicroSecondsPerAlg)
                {
                    PrintAlgorithmTerminationMessage(algorithm);
                    break;
                }

                PrintIndexColumn(currentStats.n);

                int  testCount   = 1;
                int  maxTest     = 1000000;
                long tickCounter = 0;
                while (testCount <= maxTest && TicksToMicroseconds(tickCounter) < MaxMicroSecondsPerIteration)
                {
                    _stopwatch.Restart();
                    currentStats.AlgResult = algorithm(n);
                    _stopwatch.Stop();
                    tickCounter += _stopwatch.ElapsedTicks;
                    testCount++;
                }

                double averageTimeMicro = TicksToMicroseconds(tickCounter) / testCount;

                currentStats.PrevTimeMicro = currentStats.TimeMicro;
                currentStats.TimeMicro     = averageTimeMicro;
                // Need to keep a dictionary of previous times for doubling calculation on this alg.
                currentStats.PrevTimesTable.TryAdd(currentStats.n, averageTimeMicro);

                doublingCalc(currentStats);

                PrintData(currentStats);

                // New Row
                Console.WriteLine();
            }
        }