How to use the timeit module?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction to the `timeit` Module
In Python, performance measurement can be crucial for understanding and optimizing code execution time. The `timeit` module provides a simple way to time small bits of Python code, making it invaluable for developers aiming to enhance their code's performance. Whether it's to improve a function, compare the speed of different implementations, or just satisfy curiosity about how fast your code runs, `timeit` is a handy tool.
Why Use `timeit`?
The `timeit` module is designed to provide a more accurate and fair approach for timing code execution than using the `time` module. Here's why `timeit` is preferable:
- Micro-Benchmarking: It helps in micro-benchmarking by isolating the code from external factors and measuring the time taken for execution.
- Automatic Repetition: It automatically repeats tests to obtain reliable timing results.
- Overhead Consideration: It minimizes the overhead added by the timing process, giving a clearer picture of the actual execution time.
Basic Usage
To use the `timeit` module, import it and create a simple statement or function you want to measure. The module provides two main methods: `timeit.timeit()` and `timeit.repeat()`.
The `timeit.timeit()` Method
The `timeit()` method runs a single statement multiple times and returns the total time taken in seconds.
- `stmt`: It takes a statement as a string. This is the code you want to time.
- `number`: Specifies the number of times to execute the statement.
- It returns the total time of executing the statement repeated by `number` times.
- `repeat`: It specifies the number of trials. This method measures the time for executing the code `number` times, repeated `repeat` times.
- It provides an array of execution times, which can be useful to assess the variance across trials.
- Use Appropriate Setup: Always provide any necessary imports and setup code via the `setup` parameter.
- Choose the Right Number: The `number` parameter should be large enough to get meaningful results but not too high to cause unnecessary waiting.
- Consider Warm-Up Overheads: Ignore initial runs if you suspect that the code warms up (for example, JIT compilation in interpreters that support it).
- Account for Variability: Use `repeat` multiple times to account for system variability and cache effects.
Related reading
- How to use WeakReference in Java and Android development?
- How to work out the complexity of the game 2048?
- how to write a recurrence relation for a given piece of code
- How upgrade a docker image without creating new image?
- How to use XPath in Python?
- How to validate a url in Python? Malformed or not
- HPA creates more pods than expected
- Hungarian Algorithm finding minimum number of lines to cover zeroes?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.