How to check the execution time of Python script?


The python script is the file that can store the code which is used to execute a specific task or set of tasks. This file is saved with the file extension “.py”. The python script can be created and edited in any text editor and can be executed using the command line prompt or can be imported as a package or module into the integrated development environment(IDE).

Each python script will take some time to execute the file; it can be calculated by using the following ways.

Using the time Module

In python, we have the time module which is used to measure the time taken by the block of code to execute. The time() function of the time module returns the time in seconds and then calculate the difference of start time and end time to get the execution time of the given code block.

Example

In this example, we will create the python script and check the execution time by using the time() function of the time module.

In the code we will create the python script and import script into python environment, then calculated the execution time of the script.

import python_sample as ps
import time
start_time = time.time()
script = ps.python_script("Welcome to Tutorialspoint")
print(script)
end_time = time.time()
execution_time = start_time - end_time
print("Execution time:",execution_time)

Output

The execution time of the given python script is as follows.

Welcome to Tutorialspoint
Execution time: -0.0009975433349609375

Example

Let’s see another example to calculate the execution time using the time module time() function.

import python_sample as ps
import time
start_time = time.time()
statement = "Python is the most popular programming language"
print(statement)
end_time = time.time()
execution_time = start_time - end_time
print("Execution time:",execution_time)

Output

The execution time of the given python code snippet is as follows.

Python is the most popular programming language
Execution time: -0.0009970664978027344

Using the timeit module

The timeit is another module which provides more accurate way to measure the execution time of the code snippet by running it multiple times and finding the average of all the time results.

The timeit module provides the function timeit, which takes the script as the input and calculates the execution time; and it returns in the seconds format.

Example

The following is the example to calculate the execution time of the python script.

import python_sample as ps
import timeit
script = ps.python_script("Welcome to Tutorialspoint,Have a happy learning")
print(script)
execution_time = timeit.timeit(number = 50)
print("Execution time:",execution_time)

Output

The following is the output of the execution time calculated of the python script using the timeit function of the timeit module.

Welcome to Tutorialspoint,Have a happy learning
Execution time: 1.100008375942707e-06

Using cProfile module function

The cProfile is another module available in python, which finds the execution time of the python script. The cProfile has the function run(), which calculates the time spent on each function execution along with the entire python script execution time.

Example

Following is the example of calculating the execution time using the cProfile module, run() function.

import cProfile
cProfile.run('python_sample')

Output

       3 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Example

Let’s see one more example to calculate the execution time of the python script using the cProfile module run() function.

import cProfile
import sample
cProfile.run('sample')

Output

The below is the output of the above code.

  3 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 :1()
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Updated on: 09-Aug-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements