Python calculation code block time overhead method

In many cases, we need to calculate the time overhead of our program. Here are two methods for calculating the time overhead.

time time() method

time time() returns the timestamp of the current time ( floating point seconds elapsed since the 1970 epoch ).
Code example:

#!/usr/bin/python
import time

time0 = time.time()
print("time0:",time0)

#输出格式转换
print("true time1:",time.asctime( time.localtime(time0) )) 
print("true time2:",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time0)) )

time.sleep(4.5)

print ("time1:",time.time()-time0)

Show results:
insert image description here

time clock() method

When time.clock() is called for the first time, it returns the actual running time of the program;

After the second call, the return isThe time interval since the first call to this call

Note: In the win32 system, this function returns the real time (wall time), while in Unix/Linux it returns the CPU time.
Code example:

#!/usr/bin/python
import time

time0 = time.clock()
print("time0:",time0)

time.sleep(4.5)

print("time1:",time.clock())

Show results:
insert image description here


Reference: rookie tutorial:
https://www.runoob.com/python/python-date-time.html
https://www.runoob.com/python/att-time-clock.html
https://www.runoob .com/python/att-time-time.html

Guess you like

Origin blog.csdn.net/TommyXu8023/article/details/105704645