You can use this Stopwatch wrapper:
public class Benchmark : IDisposable
{
private readonly Stopwatch timer = new Stopwatch();
private readonly string benchmarkName;
public Benchmark(string benchmarkName)
{
this.benchmarkName = benchmarkName;
timer.Start();
}
public void Dispose()
{
timer.Stop();
Console.WriteLine($"{benchmarkName} {timer.Elapsed}");
}
}
Usage:
using (var bench = new Benchmark($"Insert {n} records:"))
{
... your code here
}
Output:
Insert 10 records: 00:00:00.0617594
For advanced scenarios, you can use BenchmarkDotNet or Benchmark.It or NBench