python three ways to record program run time

  • 2020-06-12 09:49:23
  • OfStack

python three ways to record program run time

Here provides three methods of python to record the running time of the program, with the implementation code, and finally compare, please refer to the following:

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

Both methods 1 and 2 contain the time other programs use 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 of program running

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: