DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

Python Program to Measure the Elapsed Time in Python

ItsMyCode |

There are multiple ways to measure the elapsed time in Python. The modules that are available are time , timeit , and *Datetime * to measure elapsed time.

Using time Module

We can use the time module to calculate the time elapsed in executing a code or a method depending on your need. There are four steps involved to measure the time elapsed while executing the code in the time module.

Step 1: Import the time module

Step 2: Save the time stamp to the variable at the begining of the code execution using the time.perf_counter() function

Step 3: Save the time stamp to the variable at the end of the code executing using the time.perf_counter() function

Step 4: Print the difference between the end and start times to get the actual execution time.

Example of using time Module

# import time module
import time

# start the time and capture it in a variable
start = time.perf_counter()

# Program to read the entire file (absolute path) using read() function
with open("C:/Projects/Tryouts/python.txt", "r") as file:
    content = file.read()
    print(content)
    file.close()

# capture the end time and store it in a variable
end = time.perf_counter()

print('The total time taken to execute code is ', end - start)

Enter fullscreen mode Exit fullscreen mode

Output

Hello
Welcome to Python Tutorial
Cheers
Appending the content
Python

The total time taken to execute code is 0.05468999221
Enter fullscreen mode Exit fullscreen mode

Using timeit Module

The timeit module is often used to measure the elapsed time of smaller code snippets. We can also use the timeit() function, which executes the anonymous function with several executions.

# import timeit module
import timeit

# start the time and capture it in a variable
start = timeit.default_timer()

# Program to read the entire file (absolute path) using read() function
with open("C:/Projects/Tryouts/python.txt", "r") as file:
    content = file.read()
    print(content)
    file.close()

# capture the end time and store it in a variable
end = timeit.default_timer()

print('The total time taken to execute code is ', end - start)

Enter fullscreen mode Exit fullscreen mode

Output

Hello
Welcome to Python Tutorial
Cheers
Appending the content
Python

The total time taken to execute code is 0.005783799999999999
Enter fullscreen mode Exit fullscreen mode

The timeit.timeit() function can take another function as an argument, and it can execute the method multiple times by specifying the value into the number argument.

# import timeit module
from os import read
from time import sleep
import timeit

def readfile():
    sleep(2)
    # Program to read the entire file (absolute path) using read() function
    with open("C:/Projects/Tryouts/python.txt", "r") as file:
        content = file.read()
        file.close()
    return content

t = timeit.timeit(lambda: readfile(), number=10)

print('The total time taken to execute code is ', t)

Enter fullscreen mode Exit fullscreen mode

Output

The total time taken to execute code is 20.1075041
Enter fullscreen mode Exit fullscreen mode

Usually, to calculate the performance, we need to execute the code multiple times and get the average performance. We can acheive it using timeit.repeat() function as shown below.

# import timeit module
from os import read
from time import sleep
import timeit

def readfile():
    sleep(1)
    # Program to read the entire file (absolute path) using read() function
    with open("C:/Projects/Tryouts/python.txt", "r") as file:
        content = file.read()
        file.close()
    return content

t = timeit.repeat(lambda: readfile(), number=10, repeat=5)

print('The total time taken to execute code is ', t)

Enter fullscreen mode Exit fullscreen mode

Output

The total time taken to execute code is [10.1566243, 10.102775400000002, 10.128235400000001, 10.065340800000001, 10.076453699999995]
Enter fullscreen mode Exit fullscreen mode

The post Python Program to Measure the Elapsed Time in Python appeared first on ItsMyCode.

Top comments (0)