Icing.Diagnostics.Algorithm.BenchmarkAndCacheExecutionTime C# (CSharp) Method

BenchmarkAndCacheExecutionTime() public method

Benchmarks and caches (in ExecutionTime) the execution time stats of the algorithm.
public BenchmarkAndCacheExecutionTime ( int numberOfIterations, bool reportIndividualIterations ) : Stats
numberOfIterations int The number of iterations to run.
reportIndividualIterations bool /// If set to true, reports individual iteration stats; if false, reports average iteration stats. /// If the algorithm runs really fast, the floating point calculations will come out to zero, so you will want to set this to false. ///
return Stats
        public Stats BenchmarkAndCacheExecutionTime(int numberOfIterations, bool reportIndividualIterations)
        {
            //			ExecutionTime = new Stats(numberOfIterations, d => TimeSpan.FromMilliseconds(d).ToString(@"ss\.fffffff") + " sec", d => (d / 1000).ToString() + " sec");
            Stats.Formatter formatter =
                d =>
                {
                    string format = (d >= 1000 * 60 ? (d >= 1000 * 60 * 60 ? (d >= 1000 * 60 * 60 * 24 ? @"dd\." : "") + @"hh\:" : "") + @"mm\:" : "") + @"ss\.fff";
                    return TimeSpan.FromMilliseconds(d).ToString(format);
                };
            ExecutionTime = new Stats(numberOfIterations, formatter, formatter);

            ExecutionTime = reportIndividualIterations
                                    ? Benchmark_ReportIndividualIterations(numberOfIterations, ExecutionTime)
                                    : Benchmark                           (numberOfIterations, ExecutionTime);

            return ExecutionTime;
        }

Usage Example

Ejemplo n.º 1
0
        public void BenchmarkAndCacheExecutionTime_ReportIndividualIterations()
        {
            Algorithm algorithm = new Algorithm(() => Thread.Sleep(10));

            Stats executionTime = algorithm.BenchmarkAndCacheExecutionTime(100, true);

            Assert.AreEqual(100, executionTime.TotalIterations);
            Assert.IsTrue(1000 < executionTime.Total && executionTime.Total < 2000, "Actual execution time: " + executionTime.Total);
            Assert.IsTrue(executionTime.Min < executionTime.Average);
            Assert.IsTrue(executionTime.Average < executionTime.Max);
        }