Python timeit tutorial shows how to use the timeit module to measure the execution time of small code snippets in Python.
last modified February 16, 2025
In this article, we show how to use the timeit module in Python to measure the execution time of small code snippets. The timeit module is particularly useful for performance testing and optimization, as it provides accurate timing results by running the code multiple times.
The timeit module is part of Python’s standard library, so no additional installation is required.
The timeit module provides the timeit function, which can be used to measure the execution time of a statement. The function has the following parameters:
stmt: The statement to be timed (default is pass).
setup: Code to run before executing the statement (default is pass).
timer: A timeit.Timer object (usually has a sensible default).
number: The number of times to execute the statement (default is 1,000,000).
The following example demonstrates how to time a simple arithmetic operation.
main.py
import timeit
stm = “5 + 5” print(timeit.timeit(stmt=stm, number=50_000_000))
In this program, the timeit.timeit function is used to measure the execution time of the statement 5 + 5 over 50 million iterations.
$ python main.py 0.7231783000170253
The following example compares the execution time of two methods for calculating exponentiation: using the ** operator and the pow function.
main.py
import timeit
stm = “3 ** 3” print(timeit.timeit(stmt=stm, number=50_000_000))
stm = “pow(3, 3)” print(timeit.timeit(stmt=stm, number=50_000_000))
In this program, the timeit.timeit function is used to measure the execution time of the statements 3 ** 3 and pow(3, 3) over 50 million iterations.
$ python main.py 0.6393674000282772 3.6729515999904834
The following example compares the execution time of a for loop and a while loop.
main.py
import timeit
mycode = ’’’ def fn(): for e in range(10000): print(e) ’''
mycode2 = ’’’ def fn(): i = 0 while i < 10000: print(i) i += 1 ’''
print(timeit.timeit(stmt=mycode, number=5_000_000))
print(timeit.timeit(stmt=mycode2, number=5_000_000))
In this program, the timeit.timeit function is used to measure the execution time of a for loop and a while loop over 5 million iterations.
$ python main.py 0.44675440003629774 0.4859266999992542
The following example demonstrates how to create a decorator using the timeit module to measure the execution time of functions.
main.py
import timeit
def time_it(fn): def wrapper(*args, **kwargs): t0 = timeit.default_timer() fn(*args, **kwargs) t1 = timeit.default_timer() print("{0:.10f} seconds".format(t1 - t0)) return wrapper
@time_it def fstring_fn(vals): print(“fstring_fn:”, f"{vals[0]} {vals[1]} {vals[2]} {vals[3]} {vals[4]}")
@time_it def format_fn(vals): print(“format_fn:”, “{0} {1} {2} {3} {4}".format(*vals))
@time_it def oldschool_fn(vals): print(“oldschool_fn:”, “%s %s %s %s %s” % vals)
data = (‘sky’, ‘pen’, 23.0, -11, True)
fstring_fn(data) print(’—————’) format_fn(data) print(’—————’) oldschool_fn(data)
In this program, the time_it decorator is used to measure the execution time of three functions: fstring_fn, format_fn, and oldschool_fn.
oldschool_fn: sky pen 23.0 -11 True 0.0003093000 seconds
In this article, we have shown how to use the timeit module in Python to measure the execution time of small code snippets. The timeit module is a powerful tool for performance testing and optimization.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.