timeit module

In this article, we will see about timeit module which can be used for performance testing for small functions.

Example

import timeit

timeit.timeit(stmt="filter(None, [None. 2, 3, None, False, 4])", number=10000)

stmt is the statement or code snippet to be executed

number is the number of times to execute the statement

You can also repeat a single statement multiple times using timeit.repeat. Let us see a simple example for it.

import timeit

timeit.repeat(stmt="filter(None, [None, 2, 3, None, False, 4])", repeat=5, number=10)

The execution of the statement will be repeated for the mentioned number of times in repeat attribute and executes for number of times every time. It returns the list of time taken in seconds for executing the statement.

Leave a Reply