Python Timeit with Partial

COMPUTER PROJECTS

The timeit module provides an easy way to time python code. I use it to check if a new code implementation runs faster than an older implementation.

Example usage of this is:

import timeit

timeit.timeit('list(range(100))')

def greet():
    print('Hello World')

timeit.timeit(greet, number=4)

Timeit does not provide a means of setting function parameters. A quick work around for this is to use partial. Partial results in a new function with the arguments provided frozen.

It can be used as shown:

import timeit
from functools import partial

def double(x):
    return 2 * x

double_5 = partial(double, 5)
timeit.timeit(double_5)