This article mainly introduces the summary of Python execution time calculation methods. For more information, see this article, for more information, see
First of all, let's talk about the pitfalls and problems I encountered in production. I schedule Python script execution and monitor this process. The python script running time is much longer than the program execution time measured by myself in the python script.
It takes about 36 hours to monitor the execution of the python script, and about 4 hours to count the execution time in the python script.
The first thing that comes to mind when a problem occurs in Linux is found to be abnormal.
Then I thought of writing data asynchronously to py2neo in python, blocking process execution.
Finally, the problem is found: the time used by the python script is time. clock (). In this way, the CPU execution time is counted, not the program execution time.
Next, compare the statistical time of several python methods:
Method 1:
import datetimestarttime = datetime.datetime.now()#long running#do something otherendtime = datetime.datetime.now()print (endtime - starttime).seconds
Datetime. datetime. now () gets the current date. after the execution of the program ends, the obtained time value is the execution time of the program.
Method 2:
start = time.time()#long running#do something otherend = time.time()print end-start
Time. time () gets the current time since the epoch (in seconds ). If the system clock provides them, there may be a second score. Therefore, this field returns a floating point type. What we get here is alsoThe execution time of the program.
Method 3:
start = time.clock()#long running#do something otherend = time.clock()print end-start
Time. clock () returns the CPU time since the program starts or is called clock () for the first time. This has the same precision as system records. The return value is also a floating point type. What we get here isCPU execution time.
Note: program execution time = cpu time + io time + sleep or wait time
Thank you for reading this article. I hope it will help you. thank you for your support for this site!
The above is the details of the code using the Python execution time calculation method. For more information, see other related articles in the first PHP community!