Python calculates the run time from the beginning of the program to the end of the program and the CPU time for the program to run

  • 2020-04-02 13:15:36
  • OfStack

The execution time

Method 1


import datetime
starttime = datetime.datetime.now()
#long running
endtime = datetime.datetime.now()
print (endtime - starttime).seconds

Method 2


start = time.time()
run_fun()
end = time.time()
print end-start

Methods 3


start = time.clock()
run_fun()
end = time.clock()
print end-start

Methods 1 and 2 both contain the time that other programs spend on the CPU, which is the running time from the beginning of the program to the end of the program.
Method 3 only calculates the CPU time the program runs


Related articles: