Several calculation methods of Python execution time

let me start by saying a few things about the pits I ran into, the production problems I ran into, the fact that I was scheduling Python scripts to execute and monitoring the process, and that Python scripts took much longer to execute than the Python scripts themselves.
monitor python script execution time is 36 hours , while python script statistics their execution time time is 4 hours or so.
problem after the first thought was that there was a problem with Linux, looking for various logs did not find any exceptions.
is then thought of in python as py2neo writing data asynchronously, blocking the execution of the process. Finally, the problem was identified: the python script USES time.clock(), which counts the CPU execution time, not the program execution time. Next, compare several python time statistics:

method 1:

import datetime
starttime =
#long running
#do something other
endtime =
print (endtime - starttime).seconds

datetime. Datetime. Now () gets the current date, which is execution time after the execution of the program.

method 2:

start = time.time()
#long running
#do something other
end = time.time()
print end-start

time.time() gets the current time, in seconds, since epoch. If the system clock provides them, fractions of seconds may exist. So this is going to return a floating point type. This is also the program execution time .

method 3:

start = time.clock()
#long running
#do something other
end = time.clock()
print end-start

time.clock() returns the CPU time since the program started or the first time it was called clock(). This has as much precision as the system records. It also returns a floating point type. What you get here is CPU execution time . Note: program execution time = CPU time + IO time + sleep or wait time

Read More: