---Advertisement---

Python timeit() Function with Examples: Best Guide for Performance Measurement 2025

By Bhavani

Updated On:

---Advertisement---
 Python timeit() Function with Examples

👉Python timeit() Function with Examples

Python timeit() Function with Examples: The Python timeit() function is a powerful method in Python’s timeit module used to measure the execution time of small code snippets. It is especially useful for performance testing and code optimization.


👉What is Python timeit()?

Python timeit() Function with Examples: Python’s timeit() method runs a given code snippet multiple times (by default 1 million) and returns the minimum time taken. This ensures precise performance evaluation by eliminating random fluctuations.


👉Python timeit() Function with Examples Syntax

import timeit

timeit.timeit(stmt, setup, timer, number)

👉Python timeit() Function with Examples Parameters

  • stmt: Code statement to measure (default is ‘pass’).
  • setup: Code required to initialize before executing stmt (default is ‘pass’).
  • timer: Specifies the timer value (usually auto-configured by timeit()).
  • number: Specifies how many times stmt should execute (default is 1000000).

👉Using timeit() in Python

Before using timeit(), import the module as shown:

import timeit


👉Example 1: Basic Example of timeit()

import timeit

print(timeit.timeit(‘output = 10 * 5’))

👉Output:

0.061278803

This code snippet calculates the time taken to execute output = 10 * 5.


👉Example 2: Measuring Multiple Lines of Code

👉Method 1: Using Semicolon (;)

import timeit

print(“Time taken:”, timeit.timeit(stmt=’a=10; b=10; sum=a+b’))

👉Output:

Time taken: 0.137031482

👉Method 2: Using Triple Quotes (”’)

import timeit

import_module = “import random”

testcode = ”’

def test():

    return random.randint(10, 100)

”’

print(timeit.repeat(stmt=testcode, setup=import_module))

👉Output:

[0.43638873, 0.504093968, 0.50691709, 0.394344933, 0.354688697]


👉timeit() Methods

Python’s timeit module offers additional methods:

  • timeit.default_timer(): Returns the current time in seconds since a reference point.
  • timeit.repeat(): Repeats the execution multiple times and returns an array of results.

👉Example Using default_timer()

import timeit

import random

def test():

    return random.randint(10, 100)

start_time = timeit.default_timer()

print(“Start Time:”, start_time)

test()

print(“Time Difference:”, timeit.default_timer() – start_time)

👉Output:

Start Time: 0.220261875

Time Difference: 0.000473732

👉Example Using timeit.repeat()

import timeit

import_module = “import random”

testcode = ”’

def test():

    return random.randint(10, 100)

”’

print(timeit.repeat(stmt=testcode, setup=import_module, repeat=5))

👉Output:

[0.43638873, 0.504093968, 0.50691709, 0.394344933, 0.354688697]


👉Using timeit() in Command-Line Interface (CLI)

Python’s timeit() can also be executed directly from the command line.

👉Syntax:

python -m timeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [code statement …]

👉Example:

C:\pythontest>python -m timeit -s ‘text=”hello world”‘

20000000 loops, best of 5: 13.1 nsec per loop


👉Why Use timeit() for Performance Testing?

  • It repeats code execution 1 million times by default, providing precise timing data.
  • It disables Python’s garbage collection during testing to ensure consistent results.
  • It automatically adjusts based on your operating system (e.g., time.time() for Unix, time.clock() for Windows).

👉Summary

The Python timeit() function is a powerful tool for measuring code execution time accurately. Whether you are optimizing functions, debugging performance issues, or benchmarking algorithms, timeit() is an essential utility for every Python developer. Master its syntax, methods, and use cases to enhance your coding efficiency.

 Python map() Function with Examples

Download Python

Leave a Comment

Index