Runs a simple benchmark preceded by warm up phase. - CSharp System

CSharp examples for System:Action

Description

Runs a simple benchmark preceded by warm up phase.

Demo Code

// All rights reserved.
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System;/*from  w ww.j  ava  2 s  . c om*/

public class Main{
        /// <summary>
        /// Runs a simple benchmark preceded by warm up phase.
        /// </summary>
        public static void RunBenchmark(int warmupIterations, int benchmarkIterations, Action action)
        {
            Console.WriteLine("Warmup iterations: " + warmupIterations);
            for (int i = 0; i < warmupIterations; i++)
            {
                action();
            }

            Console.WriteLine("Benchmark iterations: " + benchmarkIterations);
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < benchmarkIterations; i++)
            {
                action();
            }
            stopwatch.Stop();
            Console.WriteLine("Elapsed time: " + stopwatch.ElapsedMilliseconds + "ms");
            Console.WriteLine("Ops per second: " + (int)((double)benchmarkIterations  * 1000 / stopwatch.ElapsedMilliseconds));
        }
}

Related Tutorials