Python timeit with Best Example

Python timeit() is a method that enables programmers to measure the execution time of their programs.

In this tutorial, you will know what and how the Python timeit function works with various example programs.

In addition to that, this site also provides more Python tutorials for Ord and Python bin functions

To begin with, let us learn…

What is timeit in python?

The timeit() function is a built-in module available in the Python programming library. This function is used to measure the time of small Python code execution.

In other words, the timeit() function simplifies the timing of tiny Python code segments. Additionally, it avoids some frequent pitfalls when measuring execution times.

See the following information to know more about the Python timeit() function.


To try how the timeit() function works, you must apply its correct syntax.

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


The timeit() function in Python takes 4 parameters:

  1. Statement (stmt) – The statement argument receives the code to be measured and returns the execution time. Its default value is “pass”.
  2. Setup – This parameter contains the setup instructions that must be executed before the statement. It has the same value as stmt by default (“pass”).
  3. Timer – The timer argument retrieves the timer’s value. Nevertheless, timeit() already has a default value, so we may disregard it.
  4. Number – The stmt will execute according to the number specified in the number argument, with 1000000 being the default value.

Now, let us apply the Python timeit() function using its syntax and parameters in the example program below.


import timeit
print(timeit.timeit('output = 1 + 1'))



Code explanation

The first line of code in the example import timeit enables the program to execute the timeit function.

print(timeit.timeit('output = 1 + 1')) is the part of the program where we apply the timeit() function.

Upon executing the simple program, it displays the execution time of 'output = 1 + 1' which is 0.12784359799991307.

The output shows a single line code’s time measurement which doesn’t even reach one second. How about we try the timeit() function with multiple lines of code?

Also read: Python Get Environment Variable with Example

Timing Multiple lines in Python code

The examples below will let you know how the Python timeit() function deals with multiple lines of code or programs.

Take note: There are two ways to try for the timeit() function to work with multiple lines and these are:

  1. Through the use of semicolons
  2. By using triple quotation marks

Example 1: Using a semicolon

import timeit
print("The time execution is",timeit.timeit(stmt='a=1;b=1;output=a+b'))


The time execution is 0.03557308299969009

Code Explanation

In example 1, the timeit() measures the time execution of stmt (statement). This statement holds the multiple lines of codes with the addition operator.

As shown in the output, the total time to execute the operation is 0.03557308299969009.

Now let us try using Python timeit() with triple quotation marks to deal with multiple lines of code.

Example 2: Using triple quotation marks

import timeit
import_module = "import random"
testcode = ''' 
def test(): 
    return random.randint(1, 10)
print(timeit.repeat(stmt=testcode, setup=import_module))


[0.12382391200026177, 0.1322068230001605, 0.13397703000009642, 0.1347540660008235, 0.11336433299948112]

Code Explanation

Due to the multiple lines of code, the program also returns multiple outputs specifying the time execution of each line.

timeit() Python Methods

Python timeit() function has the following important methods that you need to understand:

  1. timeit.default_timer() – This function depends on the platform and returns the CPU time and the waiting time. Additionally, its value would vary for Linux, Windows, and other operating systems. The output time may also be impacted by other programs that are running on the same computer.
  2. timeit.repeat() – This function works similarly with Python timeit() function. However, it goes through the process of figuring out how long the provided code will take to execute repeatedly.

Let’s take a look at the following examples to test these functions:

Example 1: Applying timeit.default_timer()

import timeit

from math import sqrt

def squaring(): 
   for x in range(50): 



Code Explanation

The example program uses the timeit.default_timer to return the time duration of the program execution.

This example also imports the sqrt module from the math class. It is applied in the sample operation which is to find the square root value of numbers ranging from 1 to 50. This execution takes the duration of 1.4597000017602113e-05 as shown in the output.

Example 2: Applying timeit.repeat()

import timeit

def squaring(): 
   for x in range(50): 

print(timeit.repeat(stmt=code,setup="from math import sqrt",number=10000,repeat=3))


[0.0010670500014384743, 0.0011105010016763117, 0.0011252419972151984]

Code Explanation

In example 2, the program is assigned to perform the squaring of numbers 1-50 just like in example 1. However, the second example demonstrates the implementation of the timeit.repeat() function.

Upon execution, the program performs the arguments and repeats them three times. Because of this, the output was shown three times to show how many times the program was run. 

timeit() Python with examples

Why timeit? 

There are several considerations why you should use the Python timeit() function.

  • First, this function will help you measure the duration of time when you execute your program. Its method automatically provides the time duration which saves your time in subtracting the start time of execution from the end time.
  • Second, though this function is inaccurate for some reason, it still provides you options to choose whether you implement the function by default (once) or use its timeit.repeat() function.
  • More importantly, the Python timeit function goes over and over your program to ensure that the timing corresponds to the execution of the argument/s.

Let us clarify the statements with the example below.


import timeit
sampleSetup = "from math import sqrt"
sampleCode = '''
def example():
    mylist = []
    for x in range(100):
print (timeit.timeit(setup = sampleSetup,
                     stmt = sampleCode,
                     number = 100000))



Code Explanation

This example now emphasizes the parameters taken by the timeit() function. The program is executed once which is why the output also displays a single duration.

According to the program execution, the time it takes is 0.01069812700006878.


In summary, this tutorial has discussed every detail needed to understand how the Python timeit() function works. Also, we include examples to clarify and expose the function in different scenarios and possibilities.

In addition to this, the Python timeit() function tutorial explained that its major purpose is to return the duration of time upon executing a program. This will help programmers with testing how quick or slow their program will respond.

Its content also adds important methods that you may use in your projects. These methods are the timeit.default_timer (using the default timer of the device or timezone) and the timeit.repeat (works the same as timeit() but repetitive functions.

Leave a Comment