private void AlgorithmTestRuntime(Algorithm algorithm, DoublingCalculator doublingCalc) { PrintHeader(algorithm); var currentStats = new AlgStats(); for (var n = NMin; n * 2 < NMax; n *= 2) { if (currentStats.TimeMicro > MaxMicroSecondsPerAlg || n * 2 < 0) // handle overflow case. { PrintAlgorithmTerminationMessage(algorithm); break; } PrintIndexColumn(n); var testData = GenerateUniqueSet(n); _stopwatch.Restart(); algorithm(testData, _rand.Next(NMin, NMax)); _stopwatch.Stop(); currentStats.PrevTimeMicro = currentStats.TimeMicro; currentStats.TimeMicro = TicksToMicroseconds(_stopwatch.ElapsedTicks); doublingCalc(n, currentStats); PrintData(n, currentStats); // New Row Console.WriteLine(); } }
private void AlgorithmTestRuntime(Algorithm algorithm, DoublingCalculator doublingCalc) { PrintHeader(algorithm); var currentStats = new AlgStats(); for (var n = NMin; n <= NMax; n *= 2) { 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) { Graph graph = new EuclideanCircularGraph(n, 100); _stopwatch.Restart(); currentStats.AlgResult = algorithm(graph); _stopwatch.Stop(); // HACK (this should be handled better) currentStats.CorrectnessRatio = ((EuclideanCircularGraph)graph).ShortestRouteCost / (double)currentStats.AlgResult; 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(); } }
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(); } }
// Called from within the scope of your algorithms instantiation, simply pass the algorithm function name // and the doublingcalculator function name as parameters. Call RunTimeTests to run each algorithm added // and display statistics based on doubling calculator. public void AddAlgorithmToBenchmark(Algorithm algorithm, DoublingCalculator doublingCalc) { _algorithms.Add(algorithm); _doublingCalculators.Add(doublingCalc); }