Simple easy .NET benchmarking for little bits of code. When you just really want to see if one method is actually faster than another.
Run the following command in the Package Manager Console (NuGet).
PM> Install-Package Benchmark.It
Or clone and include BenchmarkIt.csproj directly
Lets say you wanted to see if string.Contains was faster or slower than string.IndexOf. Simply write the following and have it printed out nicely for you to see.
Benchmark.This("string.Contains", () => "abcdef".Contains("ef"))
.Against.This("string.IndexOf", () => "abcdef".IndexOf("ef"))
.For(5)
.Seconds().PrintComparison();
Name Iterations Percent
Name Iterations Percent
string.Contains 23117812 100%
string.IndexOf 10852501 46.9%
Or you wanted to see if a foreach loop was actually faster than a for loop (it is).
int[] values = Enumerable.Range(1, 100000).ToArray();
Benchmark.This("for.Count", () =>
{
for (int i = 0; i < values.Count(); i++)
{
int x = values[i];
}
})
.Against.This("for.Length", () =>
{
for (int i = 0; i < values.Length; i++) {
int x = values[i];
}
})
.Against.This("foreach", () =>
{
foreach (int x in values);
})
.For(10000)
.Iterations()
.PrintComparison();
Name Milliseconds Percent
for.Count 46116 9581%
for.Length 621 129%
foreach 481 100%
And why stop there, you can add as many different methods as you want.
Benchmark.This("empty 1", () => { })
.Against.This("empty 2", () => { })
.Against.This("empty 3", () => { })
.Against.This("empty 4", () => { })
.Against.This("empty 5", () => { })
.Against.This("empty 6", () => { })
.For(1).Minutes().PrintComparison();
You can also just benchmark one method, and on top of that you can specify a number of warmpup loops to perform first.
Benchmark.This("string.Contains", () => "abcdef".Contains("ef"))
.WithWarmup(1000)
.For(5).Seconds()
.PrintComparison();
- Improve result print, clean it up etc.
- Investigate manually unrolling the benchmark loops a bit
- Speed up time loop
- Generate graphs?