Performance deep dive

Pangram
Pangram in Rust

In this approach, we'll find out how to most efficiently determine if a string is a Pangram in Rust.

The approaches page lists four idiomatic approaches to this exercise:

  1. Using all with contains on lowercased letters
  2. Using HashSet with is_subset
  3. Using HashSet with len
  4. Using a bit field to keep track of used letters

Benchmarks

To benchmark the approaches, we wrote a small benchmark application.

test is_pangram_all_contains   ... bench:         850 ns/iter (+/- 525)
test is_pangram_hash_is_subset ... bench:       3,113 ns/iter (+/- 188)
test is_pangram_hashset_len    ... bench:       1,737 ns/iter (+/- 331)
test is_pangram_bitfield       ... bench:          76 ns/iter (+/- 12)
  • The HashSet len approach is not quite twice as fast as the HashSet is_subset approach.
  • The all contains approach is about twice as fast as the HashSet lenapproach.
  • The bit field approach is more than ten times as fast as the all contains approach.
  • So, the bit field approach is about forty times faster than the HashSet is_subset approach.
28th Aug 2024 · Found it useful?