Get the App
SLTechnology News&Howtos  ›  Development  › 

How to quickly calculate the running time of functions by python

Shulou Source: shulou.com Published: 2022-06-01 05:26:13 10月03日 Update

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!

Tags: Run time function line code content article value interest guy partner method more appearance module knowledge result industry information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker vpn OPPO Reno Shulou Information