Q:

Program to find the execution time of a program in Python

belongs to collection: Python basic programs

0

The execution time of a program is defined as the time spent by the system to execute the task. As we all know any program takes some execution time but we don't know how much. So, don't worry, in this tutorial we will learn it by using the datetime module and also we will see the execution time for finding the factorial of a large number. A large number will be provided by the user and we have to calculate the factorial of a number, also we have to find the execution time of the factorial program. Before going to write the Python program, we will try to understand the algorithm.

Algorithm to find the execution time of a factorial program:

  1. Initially, we will import the datetime module and also the math module(to find the factorial) in the Program. Take the value of a number N from the user.
  2. Take the value of a number N from the user.
  3. Find the initial time by using now() function and assign it to a variable which is t_start.
  4. Calculate the factorial of a given number(N) and print it.
  5. Here, we will also find the current time and assign it to a variable which is t_end.
  6. To know the execution time simply find the difference between the t_end and t_start i.e t_end - t_start.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Now, let's start writing the Python program by simply implementing the above algorithm.

# 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: 25
factorial of the number: 15511210043330985984000000
The execution time for factorial program: 0:00:00.000022

The output format of the execution time of factorial as "hours: minutes: seconds. microseconds".

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Program to find the x-intercept and y-intercept of... >>
<< Find the N-th number which is both square and a cu...