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.
Usage Example
Section titled “Usage Example”# Correct way to run benchmarksdotnet 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;}Output console
Section titled “Output console”dotnet run -c Release --filter *Fixed*// BenchmarkDotNet outputs results without warnings.Important notes
Section titled “Important notes”- 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.).
Real‑world usage example
Section titled “Real‑world usage example”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.