测量Python中经过的时间?


测量Python中经过的时间?


使用time模块

import time

start = time.time()
print("hello")
end = time.time()
print(end - start)

Python3

import time

t = time.process_time()
#do some stuff
elapsed_time = time.process_time() - t

timeit模块

from timeit import default_timer as timer

start = timer()
# ...
end = timer()
print(end - start) # Time in seconds, e.g. 5.38091952400282