Last Updated: June 6, 2026
timeit measures how long a small piece of Python code takes to run. It is built for comparing two implementations of the same operation, like whether a list comprehension or a map call is faster, or whether dict.get beats try/except KeyError on missing keys. This lesson covers why time.time() is the wrong way to do these comparisons, how to use timeit from the command line and in code, the Timer.repeat pattern for handling variance, and the cases where cProfile is the better choice.