Program to find the execution time of a program in Python
In this example, we will see a Python program through which we can find the execution time of any Python program.
The execution time of a program is defined as the time taken by the system to execute the given task.
Algorithm:
- STEP 1: Initially, we will import the datetime module and also the math module in the Program.
- STEP 2: Take the value of a number N from the user.
- STEP 3: Find the initial time by using now() function and assign it to a variable “t_start”.
- STEP 4: Calculate the factorial of a given number(N) and print it.
- STEP 5: Find the current time and assign it to a variable “t_end”.
- STEP 6: The execution time is equal to the difference between t_end and t_start ( t_end - t_start).
Program:
# importing the modules
from datetime import datetime
import math
N=int(input("Enter the value of N: "))
t_start=datetime.now()
s=math.factorial(N)
print("factorial of the number:",s)
t_end=datetime.now()
e=t_end-t_start
print("The execution time for factorial program: ",e)
Output:
Enter the value of N: 45
factorial of the number: 119622220865480194561963161495657715064383733760000000000
The execution time for factorial program: 0:00:00.000091