timeit

Stdlib — ProfilingPython 2.0+Intermediate

Measure execution time of small code snippets

Quick Info

Documentation
Official Docs
Python Version
2.0+
Dependencies
None — Python Standard Library
Install
Included with Python

Learn by Difficulty

Quick Example

python
import timeit

# Time a simple expression
t1 = timeit.timeit("sum(range(100))", number=10000)
t2 = timeit.timeit("[x**2 for x in range(100)]", number=10000)
print(f"sum(range(100)): {t1:.4f}s")
print(f"List comp: {t2:.4f}s")

The timeit module is part of Python's standard library. Measure execution time of small code snippets.

Try in Playground

Tags

stdlibprofilingperformance