In this approach, we'll find out how to most efficiently determine if a string is a Pangram in Python.
The approaches page lists three idiomatic approaches to this exercise:
For our performance investigation, we'll also include a fourth approach that uses a bit field to keep track of used letters.
Benchmarks
To benchmark the approaches, we wrote a small benchmark application using the timeit library.
all: 1.505466179997893e-05
all: 1.6063886400021147e-05 // with sentence.casefold()
set: 1.950172399985604e-06
len: 3.7158977999933994e-06
bit: 8.75982620002469e-06
- The
setlen()approach is not as fast as thesetissubset()approach. - The
all()approach is slower than eithersetapproach. Usingcasefoldwas slower than usinglower. - Although the bit field approach may be faster in other languages, it is significantly slower in Python.
It is faster than the
all()approach, but much slower than eithersetapproach.
4th Sep 2024
·
Found it useful?