Write a program factorial_execution_time.py
that includes a function factorial(n)
which uses a loop to calculate the factorial of the value n
. Import the time
module into your program, and use it to measure the total execution time of the calculation.
start_time = time.time()
factorial(n)
stop_time = time.time()
time_elapsed = stop_time - start_time
Finally, include your measurement in a loop so that the experiment can be performed multiple times and the results compared.
Sample Output
Calculating the factorial value of 10000!
It took 0.021396160125732422 seconds to calculate 10000!
It took 0.01759195327758789 seconds to calculate 10000!
It took 0.017870664596557617 seconds to calculate 10000!
It took 0.01864790916442871 seconds to calculate 10000!
It took 0.019140005111694336 seconds to calculate 10000!
It took 0.018581867218017578 seconds to calculate 10000!
It took 0.01799297332763672 seconds to calculate 10000!
It took 0.017860889434814453 seconds to calculate 10000!
It took 0.0180511474609375 seconds to calculate 10000!
It took 0.01776719093322754 seconds to calculate 10000!