Write a program to get execution time (in seconds) for a Python method.
import time def sum_of_n_numbers(n): start_time = time.time() s = 0 for i in range(1,n+1): s = s + i end_time = time.time() return s,end_time-start_time n = 5 print("nTime to sum of 1 to ",n," and required time to calculate is :",sum_of_n_numbers(n))
Time to sum of 1 to 5 and required time to calculate is : (15, 2.384185791015625e-06)
Python Exercise: Write a program to get execution time (in seconds) for a Python method
Free Machine Learning & Data Science Coding Tutorials in Python & R for Beginners. Subscribe @ Western Australian Center for Applied Machine Learning & Data Science.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.