Explain Codes LogoExplain Codes Logo

How to use timeit module

python
benchmarking
performance
best-practices
Anton ShumikhinbyAnton Shumikhin·Oct 18, 2024
TLDR

Measure execution time of your Python code using timeit.timeit(). Pass your code in a string, and specify iterations with number:

import timeit execution_time = timeit.timeit('sum(range(100))', number=1000) print(execution_time)

The above measures the time to execute sum(range(100)) 1000 times. Dial up number for better precision.

Precision timing of functions

Handle function-specific timing by directly invoking timeit.Timer(). Use functools.partial to curb the timer's impact:

from timeit import Timer from functools import partial def my_function(): # Your next "Big Thing" code here pass # Looks like it's time for my function! timed_run = Timer(partial(my_function)).timeit(number=10000) print(timed_run)

Here, partial helps to lower the timing overhead.

Prepping the stage with setup

setup lets you reset starting conditions to keep data states from messing with results. It's a lifesaver for tests like in-place sorting:

import timeit setup_code = """ import random my_list = list(range(100)) random.shuffle(my_list) """ test_code = """ sorted(my_list) """ # Freshly unsorted data served up for each test times = timeit.repeat(stmt=test_code, setup=setup_code, repeat=5, number=1000) print(min(times)) # Keep the best time, throw away the rest

Juggling IPython and command-line integration

In IPython, %timeit eases syntax:

%timeit sum(range(100))

On the command line, use -m timeit:

python -m timeit -s "import random" "random.shuffle(list(range(100)))"

The -s flag sets initial conditions, ensuring consistent tests.

Running a function efficiency contest

Assess various implementations by comparing their time efficiency. It's like a code beauty contest, with beauty being superlative performance:

import timeit def using_list_comprehension(): return [i for i in range(100)] def using_map_function(): return map(lambda i: i, range(100)) def using_for_loop(): result = [] for i in range(100): result.append(i) # Not another for-loop! return result # Let the contest begin! times = { 'List Comprehension': timeit.timeit(using_list_comprehension, number=10000), 'Map Function': timeit.timeit(using_map_function, number=10000), 'For Loop': timeit.timeit(using_for_loop, number=10000), } # Looksy: the timing results, sorted! print(sorted(times.items(), key=lambda x: x[1]))

Dive deeper, go further

For advanced benchmarking:

  • Play out multiple suite runs with timeit.repeat and fix eyes on the shortest time.
  • Scramble data in between runs to dodge timing biases.
  • Learn how Python's standard library's Timsort fares on partially ordered data.
  • Deep-dive into the documentation for a more profound grasp on timeit.