time模块记时

time模块记时,第1张

time模块记时
import time

start = time.perf_counter()  # 开始时间
# ----需测试时间长度的代码块----
end = time.perf_counter()   # 结束时间

print(end-start)

可以测出for循环比while循环效率高
因为for循环中的range由C语言编写,python解释器也C语言编写
而while +1-1需要间接调用C语言代码,而且要对象创建和删除的底层 *** 作
所以while效率低

import time

start = time.perf_counter()  # 开始时间

i = 0
while i < 100000000:
    i += 1

# for i in range(0,100000000):
#     pass

end = time.perf_counter()   # 结束时间

print(end-start)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5715201.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存