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:
- 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.
- Take the value of a number N from the user.
- Find the initial time by using now() function and assign it to a variable which is t_start.
- Calculate the factorial of a given number(N) and print it.
- Here, we will also find the current time and assign it to a variable which is t_end.
- To know the execution time simply find the difference between the t_end and t_start i.e t_end - t_start.
Now, let's start writing the Python program by simply implementing the above algorithm.
Output
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