VOOZH about

URL: https://www.analyticsvidhya.com/blog/2020/09/timeit-in-python/

⇱ timeit in Python | Getting Started With timeit Library


India's Most Futuristic AI Conference Is Back – Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Start Using The timeit Library in Python!

Guest Blog Last Updated : 27 Oct, 2024
5 min read

Introduction

The purpose of this article is to introduce us to the timeit library.

πŸ‘ Image

Suppose you want to measure the execution time of a code snippet. What do you do? Till now, I just like most people would do something like this:

Python Code:

import time 
start_time = time.time() 
"""Some Code""" 
end_time = time.time() 
print(f"The execution time is: {end_time-start_time}")

Now say we want to compare the execution times of two different functions, then:

import time
def function_1(*parameters):
 #do something
 # return value
def function_2(*parameters):
 #do something
 #return value
 
#measuring time taken by function_1
start_of_f1 = time.time()
value1 = function_1(*parameters)
end_of_f1 = time.time()
print(f”The time taken by function_1 is:{start_of_f1-end_of_f1}”)#measuring time teaken by function_2
start_of_f2 = time.time()
value2 = function_1(*parameters)
end_of_f2 = time.time()print(f”The time taken by function_1 is:{start_of_f2-end_of_f2}”)

This is the way most people measure execution times and compare two functions.

However, if we have to measure the time taken by a very small code snippet, this method fails to produce accurate results. Even, while comparing the functions, all the other processes and tasks happening in the OS are messing around with the time.time() instance. So it is not accurate while comparing functions.

To address this problem, Python has a timeit module that is meant to measure the execution times of small and large code snippets. Here in this article, we first take a look at the timeit library in python using some examples.

First, let us look at its overview as provided in the documentation.

timeit in Python

This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times.

This module has a function, timeit.timeit(stmt = pass, setup= pass, timer = <default timer> ,number= 1000000) .This function takes four arguments:

  1. stmt: The code snippet whose execution times is to be measured.
  2. setup: The code to run before executing stmt. Generally, it is used to import some modules or declare some necessary variables. It will become more clear with the examples.
  3. timer: It is a default timeit.Timer object. It has a sensible default value to it, so we do not have to do much about it.
  4. number: It specifies the number of times stmt will be executed.

It will return the time taken (in seconds) to execute the stmt, the specified number of times.

Let us look at some examples to understand it better.

import timeit

#This code will be executed only once, before stmt.
setup_code = "from math import sqrt"

#This code will be executed as specified by the parameter 'number'
stmt_code = "sum(sqrt(x) for x in range(1,10000))"
iterations = 10000
time = timeit.timeit(stmt = stmt_code, setup = setup_code, number = iterations)

print(time)

#17.9 seconds
#THIS OUTPUT IS FOR 10000 ITERATIONS OF THE CODE SNIPPET AND NOT A SINGLE ITERATION.

print(f"The time for single iteration is {time/iterations}")

All we did was to pass the code as a string and specify the number of iterations to the timeit.timeit function.

Now let us look at a way to use this amazing library to compare two functions.

For this example, let us compare two sorting functions, one is a simple bubble sort, and the other is the Python’s built-in sorted() function. Here we will also see another timeit function called repeat().

def bubble_sort(arr):
 for i in range(len(arr)):
 for j in range(0, len(arr) - i - 1):
 if arr[j] > arr[j + 1]:
 arr[j], arr[j + 1] = arr[j + 1], arr[j]
 return arr


def make_random_array():
 from random import randint

 return [randint(-100, 100) for i in range(20)]


test_array = make_random_array()
'''
Since we want to make sure that we test our functions on the same array, we are declaring an array
here and will later call it in compare_functions().
'''

def compare_functions():
 import timeit

 setup_code1 = """
from __main__ import bubble_sort
from __main__ import test_array
"""
 setup_code2 = """
from __main__ import test_array
"""
 stmt_code1 = "bubble_sort(test_array)"
 stmt_code2 = "sorted(test_array)"

 times1 = timeit.repeat(stmt=stmt_code1, setup=setup_code1, number=10000, repeat=5)
 times2 = timeit.repeat(stmt=stmt_code2, setup=setup_code2, number=10000, repeat=5)
 print(f"Time taken by bubble sort is {min(times1)}")
 print(f"Time taken by built in sort is {min(times2)}")

if __name__ == "__main__":
 compare_functions()
"""
Time taken by bubble sort is 0.2646727000001192
Time taken by built in sort is 0.0031973000000107277
"""

Here, the output is the minimum value in the list times1 and times2.

In the setup code, we had,

from __main__ import bubble sort
from __main__ import arrThis imports the bubble_sort function already defined in our program and also import the array 'test_array' which we declared in the program earlier.
This will make sure that both the functions are tested on the same array, 'test_array'.

timeit.repeat() takes an additional parameter, repeat (default value = 5). This returns a list of execution times of the code snippet repeated a number of times.

As expected, the time taken by bubble sort is much more than the built-in sorted() function.

Now to wrap up, let us look at how we can use the command-line interface in this library.

When called as a program from the command line, the following form is used:

python -m timeit [-n N] [-r N] [-u U] [-s S][statement ...]

Here,

  1. -n N, How many times to execute the β€˜statement.’
  2. -r N, How many times to repeat
  3. -s s, setup statement
  4. -u u, unit of timer output, (nano, micro, mil, sec)

Some examples as are:

$ python -m timeit -s 'text = "sample string"; char = "g"' 'char in text'
5000000 loops, best of 5: 0.0877 usec per loop
$ python -m timeit -s 'text = "sample string"; char = "g"' 'text.find(char)'
1000000 loops, best of 5: 0.342 usec per loop

This wraps up the introduction of the timeit module. I tried to cover all one needs to know to start using this module from now on.

Here is the official documentation of the module. Please take a look at it to know more about this module. I would also encourage you to take a look at the source code of this module here.

I hope you learned something from this article, and from now on, you can use this module’s functionality in your code. Peace.

About the Author

πŸ‘ timeit in python

Kushagra Bansal

I am an undergraduate student majoring in Mathematics and I am a passionate learner with keen interests in Computer Science and Astrophysics. I identify myself as a quick learner and believe in learning by practicing. New technologies like Machine learning, Deep learning, and computer vision, etc impress me a lot. I also like to read a lot.

ff

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
πŸ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
πŸ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

πŸ‘ Popup Banner
πŸ‘ AI Popup Banner