Performance deep dive

Isogram
Isogram in C#

In this approach, we'll find out how to most efficiently determine if a string is an Isogram in C#.

The approaches page lists two idiomatic approaches to this exercise:

  1. Using Distinct
  2. Using GroupBy

For our performance investigation, we'll also include a third approach: using a bit Field to keep track of used letters.

Benchmarks

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

Method Mean Error StdDev Gen0 Gen1 Allocated
Distinct 584.10 ns 11.131 ns 25.125 ns 0.2241 - 1056 B
GroupBy 1,395.80 ns 15.937 ns 13.308 ns 0.5169 0.0019 2432 B
Bitfield 33.94 ns 0.200 ns 0.167 ns - - -

The GroupBy approach is an idiomatic "one-liner", but it is more than twice as slow as the Distinct approach, which is also idiomatic and perhaps more readable.

The fastest is the Bitfeld approach, but it may be considered more idiomatic of the C language than of C#. Also, it depends on all of the letters being ASCII.

4th Sep 2024 · Found it useful?