How to quickly calculate the running time of functions by python
This article mainly introduces python how to quickly calculate the running time of the function, the article is very detailed, has a certain reference value, interested friends must read it!
How to quickly calculate the running time of a function
Calculate the run time of a function, and you might do this
Import time start = time.time () # run the function end = time.time () print (end-start)
Look how many lines of code you have written to calculate the running time of the function.
Is there a way to calculate the run time more easily?
Yes.
There is a built-in module called timeit
Use it with only one line of code
Import time import timeit def run_sleep (second): print (second) time.sleep (second) # use only this line print (timeit.timeit (lambda: run_sleep (2), number=5))
The running result is as follows
10.020059824 and above are all the contents of the article "how to quickly calculate the running time of a function by python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!