time.clock() 被移除,可用time.perf_counter() 或 time.process_time()替代
前言
Python time.clock() 函数以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时,比time.time()更有用。
这个需要注意,在不同的系统上含义不同。在UNIX系统上,它返回的是”进程时间”,它是用秒表示的浮点数(时间戳)。而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。
而第二次之后的调用是自第一次调用以后到现在的运行时间。(实际上是以WIN32上QueryPerformanceCounter()为基础,它比毫秒表示更为精确)摘抄来源:RUNOOB.COM
3.8及之后的版本,使用time.clock()将报错
import time
scale = 50
print(\'执行开始\'.center(scale//2,\'*\'))
t = time.clock()
for i in range(scale+1):
a = \'*\'*i
b = \'.\'*(scale-i)
c = (i/scale)*100
t -= time.clock()
print(\'\r{:^3.0f}%[{}->{}]{:.2f}秒\'.format(c,a,b,-t),\
end=\'\')
time.sleep(0.05)
print(\'\n\'+\'执行结束\'.center(scale//2,\'*\'))
\'\'\'以下为报错内容
d:\py\111\1.py:4: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
t = time.clock()
d:\py\111\1.py:9: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
t -= time.clock()
\'\'\'
可以替换为 time.perf_counter()
import time
scale = 50
print(\'执行开始\'.center(scale//2,\'*\'))
t = time.perf_counter()
for i in range(scale+1):
a = \'*\'*i
b = \'.\'*(scale-i)
c = (i/scale)*100
t -= time.perf_counter()
print(\'\r{:^3.0f}%[{}->{}]{:.2f}秒\'.format(c,a,b,-t),\
end=\'\')
time.sleep(0.05)
print(\'\n\'+\'执行结束\'.center(scale//2,\'*\'))
\'\'\'以下为运行结果
***********执行开始**********
100%[**************************************************->]65.35秒
***********执行结束**********
\'\'\'