---
title: "How to Measure Speed of Python Code"
description: "Here is the best python example to use the timeit module. Helpful for projects and interviews."
url: https://srinimf.com/2022/07/11/how-to-measure-speed-of-python-code/
date: 2022-07-11
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2022/07/pexels-photo-1181519.jpeg?fit=1880%2C1255&ssl=1
categories: ["Python"]
tags: ["Python timeit"]
type: post
lang: en
---

# How to Measure Speed of Python Code

Speed of the python code indicates performance. The timeit module in python is inbuilt, and you can use it for this purpose.

Related[Pandas and NumPy Use Cases for Data Science Projects](https://srinimf.com/2025/05/30/pandas-and-numpy-use-cases-for-data-science-projects/)

*Python timeit*

## What is timeit module?

With the `timeit` module, we target a snippet of code and it will run that code millions of times, in order to give us the statistically most relevant value for the code execution time. Note that the default value for the number of times the code is executed is 1000000.

## Program using timeit

First step, import the timeit module, then use the methods (related to timeout) to get the execution time. Below, you’ll find two examples demonstrate the usage of it.

To illustrate, the first example uses for loop, and the second one uses lambda function. Therefore you can see the difference in execution time.

### Code example to use timeit

```
import timeit
code_to_measure = '''
def func1():
    numbers = []
    for x in range(1000):
      numbers.append(x*2 + x**2)
'''
measure = timeit.timeit(stmt = code_to_measure, number = 10000)
print("Execution time for function func1: ", measure)
print()
code_to_measure = '''
```

The output has two statements. One is the execution time of the func1, and the second is for func2.

**References**

- Learn Python: The Complete Python Programming Course

## The output

The func2 took more time than func1. The time measurement is in seconds.

```
Execution time for function func1:  0.0016275793313980103

Execution time for function func2:  0.017717315815389156

** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

## The key code of timeit

The “measure” object collects the time. It is a statistical time of 10000 times code executes internally and gets the time.

Also, read

- [Start Using The timeit Library in Python!](https://www.analyticsvidhya.com/blog/2020/09/timeit-in-python/)

Related

- [Enhance NumPy Performance: Techniques and Examples](https://srinimf.com/2024/01/28/how-to-optimize-numpy-performace-for-maximum-results/)DateJanuary 28, 2024
- [How to Debug Using ‘pdb’ in Python](https://srinimf.com/2020/08/02/how-to-debug-py-module-using-pdb/)DateAugust 2, 2020
- [7 Need to Know Python pdb Debugger Commands](https://srinimf.com/2022/03/07/7-need-to-know-python-pdb-debugger-commands/)DateMarch 7, 2022

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2022%2F07%2F11%2Fhow-to-measure-speed-of-python-code%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2022%2F07%2Fpexels-photo-1181519.jpeg%3Ffit%3D1200%252C801%26ssl%3D1&description=How%20to%20Measure%20Speed%20of%20Python%20Code)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2022/07/11/how-to-measure-speed-of-python-code/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2022/07/11/how-to-measure-speed-of-python-code/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2022/07/11/how-to-measure-speed-of-python-code/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2022/07/11/how-to-measure-speed-of-python-code/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Measure%20Speed%20of%20Python%20Code&body=https%3A%2F%2Fsrinimf.com%2F2022%2F07%2F11%2Fhow-to-measure-speed-of-python-code%2F&share=email)
-

### Like this:

Like Loading…