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:
- Using
allwithcontainson lowercased letters - Using
HashSetwithis_subset - Using
HashSetwithlen - 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
HashSetlenapproach is not quite twice as fast as theHashSetis_subsetapproach. - The
allcontainsapproach is about twice as fast as theHashSetlenapproach. - The bit field approach is more than ten times as fast as the
allcontainsapproach. - So, the bit field approach is about forty times faster than the
HashSetis_subsetapproach.
28th Aug 2024
·
Found it useful?