Stop guessing which code is faster. Use Benchmark.DotNet. It is the industry standard for measuring C# performance, handling complex issues like 'JIT Warmup' and 'CPU Frequency scaling' for you.
StopWatch is inconsistent. It doesn't handle the 'Cold Start' of the app or the background noise of the OS. Benchmark.DotNet runs your code thousands of times, calculates the standard deviation, and gives you a statistically significant result in **Nanoseconds**.
Add the `[MemoryDiagnoser]` attribute. This tells the benchmark to also track how many bytes were allocated on the heap and how many GC collections (Gen 0/1/2) were triggered. Architect Rule: A faster method that allocates 1MB is often WORSE than a slightly slower method that allocates 0 bytes.
Q: "What is 'JIT Inlining' and how does it affect benchmarks?"
Architect Answer: "The Just-In-Time (JIT) compiler can 'Inline' small functions—basically copying the body of the function directly into the caller to avoid the overhead of a 'Method Call'. If you write a benchmark for a tiny 1-line function, the JIT might inline it, making it look impossibly fast. We use the `[MethodImpl(MethodImplOptions.NoInlining)]` attribute in benchmarks if we want to measure the raw overhead of the call itself."