统计代码段执行时间的方法

方法(1)

import time


start_s=time.perf_counter()
time.sleep(5)
end_s = time.perf_counter()

exec_ms = (end_s - start_s)*1000
print('消耗 %.2f 毫秒。' % exec_ms)

方法(2)

import urllib3
from timeit import default_timer as timer


http = urllib3.PoolManager(num_pools=100, headers={'Connection': 'keep-alive'}, maxsize=100, block=True)

url = "https://www.yjsec.com"

tic = timer()
resp = http.request('GET', url)
toc = timer() - tic
print('消耗了 %0.2f 毫秒' % (toc * 1000))

print(resp.status)
print(resp.headers['Date'])
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注