Python TimeIt

timeit is an executable script for benchmarking Python one-liners.


Installation

timeit is included in all Python installations.


Usage

Try:

$ python -m timeit "'-'.join(str(n) for n in range(100))"
10000 loops, best of 5: 30.2 usec per loop

$ python -m timeit "'-'.join([str(n) for n in range(100)])"
10000 loops, best of 5: 27.5 usec per loop

$ python -m timeit "'-'.join(map(str, range(100)))"
10000 loops, best of 5: 23.2 usec per loop

Multiple quoted statements can be provided to benchmark a multi-line code snippet instead of a one-liner.


See also

Python timeit module documentation


CategoryRicottone

Python/TimeIt (last edited 2024-02-17 16:38:47 by DominicRicottone)