Measure execution time or runtime of a python program

 What is the execution time or runtime of a program?

Execution time is the time that a program (could be a programming language-based program) takes to execute in a machine. Running time might be used interchangeably with execution time. The runtime time of a program indicated that how much time it takes for a program to terminate in a machine. So, in computer science, the execution time and runtime of a program are the same. The execution time of a program may vary based on the machine. Machine 'A' will get a 0.000567 seconds execution time or runtime for a program. On the same program, machine 'B' will get 0.089844 seconds.

runtime of a python code

We can find out the execution time of a python program using the built-in package 'time'. Simple import the 'time' package from the library. This package is by default installed with python.


How can we do this (execution time or runtime)?

1. Import the 'time' package in your python code.

import time

2. Assign the start time of your program in a variable.

start_time = time.time()

3. Call your function or write your code to execute.

#your_code

4. Now, deduct the current time from the start time and print it.

execution time = time.time() - start_time
print("execution time is %s seconds " % (time.time() - start_time))



Full code for measuring execution time or runtime

import time
start_time = time.time()
#your_code
print("execution time is %s seconds " % (time.time() - start_time))


Suppose, we want to print a sentence 10 times. What will be the execution time? simply, put the print section in a function ( execute_function() ) and then, print the execute time. The code is given below.

import time

def execute_function():
for i in range(5):
print("Welcome to School of Beginners")

start_time = time.time()
execute_function()
print("execution time is %s seconds " % (time.time() - start_time))


Output

Welcome to School of Beginners

Welcome to School of Beginners

Welcome to School of Beginners

Welcome to School of Beginners

Welcome to School of Beginners

execution time is 0.0098744 seconds 


A real program example of measuring execution time or runtime

Now, we want to find out the execution time of 15 puzzle codes. We will simply put the code in a function called 'fifteen_puzzle'. Then, we will print the execution time of that program.

import time

def fifteen_puzzle():
n = 4
inv = 0
row = 0
arr = [2, 3, 4, 6, 7, 8, 5, 9, 10, 0, 1, 15, 14, 12, 13, 11]
for i in range(n):
if arr[i - 1] == 0:
row = n - i + 1
for i in range(0, len(arr), 1):
for j in range(i + 1, len(arr), 1):
if (arr[i] > arr[j] and arr[i] != 0 and arr[j] != 0):
inv += 1
print("Shape of the puzzle: ", n * n, "(", n, "*", n, ")")
print("No. of Inversion: ", inv)
if ((n % 2 == 1 and inv % 2 == 0) or (n % 2 == 0 and inv % 2 == 1 and row % 2 == 0) or (
n % 2 == 0 and inv % 2 == 0 and row % 2 == 1)):
print("Solve able")
else:
print("Not Possible")

start_time = time.time()
fifteen_puzzle()
print("execution time is %s seconds " % (time.time() - start_time))

Output

Shape of the puzzle:  16 ( 4 * 4 )
No. of Inversion:  21
Solve able
execution time is 0.045378 seconds 
Notebook: All the code of this post is run in the 'PyCharm'

Post a Comment

Previous Post Next Post