Performance deep dive

Protein Translation
Protein Translation in C#

In this approach, we'll find out how to most efficiently solve Protein Translation in C#.

The approaches page lists five idiomatic approaches to this exercise:

  1. Using Substring() with a Dictionary.
  2. Using Substring() with a switch.
  3. Using LINQ with a Dictionary.
  4. Using yield with a Dictionary.
  5. Using yield with a switch.

Benchmarks

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

Method Mean Error StdDev Gen0 Allocated
ProteinsSubstringDict 128.69 ns 0.517 ns 0.432 ns 0.0560 264 B
ProteinsSubstringSwitch 91.59 ns 1.426 ns 1.264 ns 0.0560 264 B
ProteinsLinqDict 453.59 ns 1.114 ns 1.042 ns 0.1254 592 B
ProteinsYieldDict 313.60 ns 1.537 ns 1.284 ns 0.0901 424 B
ProteinsYieldSwitch 270.33 ns 2.731 ns 2.554 ns 0.0901 424 B

Iterating by Substrings and looking up the values in a switch was the fastest approach.

4th Sep 2024 · Found it useful?