Python Code to Get Execution Time of Current Program

This Python source code calculates execution time of current program using time library.

In this program time library is used. time.time() return the time in seconds as a floating point number.

Python Source Code


import time

def some_function(n):
    for i in range(n):
        pass

start_time = time.time()
n = int(input("Enter positive integer: "))
some_function(n)
end_time = time.time()

print("Execution time is: ", end_time - start_time)

Output

Run 1:
------------------
Enter positive integer: 10
Execution time is:  1.6248564720153809 seconds

Run 2:
------------------
Enter positive integer: 100
Execution time is:  1.9238831996917725 seconds

Run 3:
------------------
Enter positive integer: 100000
Execution time is:  3.6697709560394287 seconds