I occasionally need to benchmark some code. In C#, this can easily be done using the Stopwatch class. After writing the same boilerplate Stopwatch wrapper enough times I decided to throw together a simple Benchmarker class.

public static class Benchmarker
{
    public static TimeSpan RunOnce(Action action)
    {
        Stopwatch stopwatch = Stopwatch.StartNew();
        action();
        stopwatch.Stop();
        return stopwatch.Elapsed;
    }
    
    public static IEnumerable<TimeSpan> RunMany(Action action, int runCount)
    {
        List<TimeSpan> results = new List<TimeSpan>();
        for (int i = 0; i < runCount; i++)
            results.Add(RunOnce(action));
        return results;
    }
}

This allows any code to be easily called and timed. For example, suppose you have a void Method called Foo. This can be timed via:

TimeSpan timespan = Benchmarker.RunOnce(() => Foo());

Similarly, if you want to benchmark a method that happens to return something like this

int bar = FooWithReturnValue();

you can use the above Benchmarker class via

int bar;
TimeSpan timespan = Benchmarker.RunOnce(() => bar = FooWithReturnValue());

I’ve put this on github: https://github.com/mattnedrich/tools/tree/master/csharp/Benchmarker.cs