Performance deep dive

Bob
Bob in C#

In this approach, we'll find out how to most efficiently determine the response for Bob in C#.

The approaches page lists two idiomatic approaches to this exercise:

  1. Using if statements.
  2. Using switch on a tuple.

For our performance investigation, we'll also include a third approach that uses an answer array.

Benchmarks

To benchmark the approaches, we wrote a small benchmark application using BenchmarkDotNet library.

Method Mean Error StdDev Gen0 Allocated
ResponseIfChain 124.07 ns 1.306 ns 1.158 ns 0.0577 272 B
ResponseWithSwitch 51.65 ns 0.377 ns 0.315 ns 0.0153 72 B
ResponseWithArray 51.07 ns 0.262 ns 0.245 ns 0.0153 72 B

For the purpose of benchmarking the string extension methods for the if statements approach were refactored to regular methods that take a string. For all approaches, all other static functions were refactored to instance functions. The if statements approach was over two times slower than the other two approaches. Given that the switch approach and answer array approach were equivalent for performance, the switch approach might be prefered for performance as considered the more idiomatic approach for C#.

4th Sep 2024 · Found it useful?