This article mainly introduces the Python execution time calculation method Summary of the relevant information, the need for friends can refer to the following
First of all, I encountered the pit, production problems encountered, I dispatched the Python script to execute and monitor the process, Python script run time is much larger than the Python script's own statistics of the program execution time.
The time to monitor Python script execution is 36 hours, while the Python script counts the time it takes to execute in 4 hours or so.
The first thing that comes to mind when the problem leaks is that Linux is out of the question, looking for various logs that don't find anything unusual.
Then I think of the Py2neo written data used in Python to asynchronous, blocking the process execution.
Finally, we finally find the problem: The Python script uses statistical time as Time.clock (), which counts the CPU execution time, not the execution time of the program.
Next, compare the statistical time patterns of several python:
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, the time value obtained by this method is the time the program executes.
Method 2:
Start = Time.time () #long running#do something otherend = Time.time () print End-start
Time.time () Gets the current time (in seconds) since the era. If the system clock provides them, there may be fractions of seconds. So this place returns a floating-point type. The execution time of the program is also obtained here .
Method 3:
Start = Time.clock () #long running#do something otherend = Time.clock () print End-start
Time.clock () returns the CPU time since the start of the program or the first time the clock () was called. This has as much precision as the system record. The return is also a floating-point type. The CPU execution time is obtained here.
Note: Program execution time =CPU time + IO time + hibernate or wait time
Thank you for reading, hope to help everyone, thank you for the support of this site!