Skip to content

Running benchmarks avoiding common pitfalls

Context: To get accurate benchmark results, avoid these common mistakes: running in Debug mode, attaching a debugger, having other applications running, not warming up, and using the same variable across benchmarks. BenchmarkDotNet automatically avoids most of these, but you must ensure your project is built in Release configuration and that you run the benchmark process without a debugger.

Terminal window
# Correct way to run benchmarks
dotnet run -c Release --filter *MyBenchmark*
// Pitfall: Dead code elimination
[Benchmark]
public void Pitfall()
{
int x = 42; // Compiler might remove this if not used
}
// Fix: Consume the value
[Benchmark]
public int Fixed()
{
int x = 42;
return x;
}
Terminal window
dotnet run -c Release --filter *Fixed*
// BenchmarkDotNet outputs results without warnings.
  • Use dotnet run -c Release (not Debug).
  • Do not run benchmarks in a virtual machine or under power‑saving mode.
  • Let the benchmark run undisturbed (close browsers, etc.).

Benchmarking a new algorithm – Run benchmarks on a dedicated machine with high‑performance power plan, and always compare against a baseline.
See BenchmarkDotNet best practices.