python对列表计时的方法:
创新互联公司专注于桐梓网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供桐梓营销型网站建设,桐梓网站制作、桐梓网页设计、桐梓网站官网定制、微信小程序开发服务,打造桐梓网络公司原创品牌,更为您提供桐梓网站排名全网营销落地服务。
使用“import”语句导入time包,在列表操作之前用time.time函数获取当前时间,在列表操作之后,再用time.time获取当前时间,用第二次的时间减去第一次的时间就可以了
示例如下:
执行结果如下:
更多Python知识,请关注:Python自学网!!
import time
print('按下回车开始计时,按下 Ctrl + C 暂停/停止计时。')
while True:
input("")
starttime = time.time()
print('开始')
pausetime=0
while True:
try:
totaltime=int((time.time()-pausetime-starttime))
print('【'+str(int((time.time()-pausetime-starttime)*0.66)),'进度】',int((time.time()-pausetime-starttime)/60),'分', int((time.time()-pausetime-starttime)%60), '秒',end='\r')
time.sleep(1)
except KeyboardInterrupt:
print('【' + str(int((time.time() -pausetime- starttime) * 0.66)), '进度】', int((time.time()-pausetime - starttime) / 60), '分',
int((time.time() -pausetime- starttime) % 60), '秒', '...暂停中\r\n按回车继续,输入Q停止计时',end='\r') pausetime_start=time.time()
s=input("")
if s.strip().upper()=='Q':
pausetime_end = time.time()
pausetime = pausetime_end - pausetime_start + pausetime
print('结束')
endtime = time.time()
print('总时间:', round(endtime - pausetime - starttime, 2), '秒 ')
exit(0)
elif s.strip().upper()=='':
pausetime_end=time.time()
pausetime=pausetime_end-pausetime_start+pausetime
import time
def start_sleep():
time.sleep(3)
if __name__ == '__main__':
#The start time
start = time.clock()
#A program which will run for 3 seconds
start_sleep()
#The End time
end = time.clock()
print("The function run time is : %.03f seconds" %(end-start))
# End
用python实现计时器功能,代码如下:
''' Simple Timing Function.
This function prints out a message with the elapsed time from the
previous call. It works with most Python 2.x platforms. The function
uses a simple trick to store a persistent variable (clock) without
using a global variable.
'''
import time
def dur( op=None, clock=[time.time()] ):
if op != None:
duration = time.time() - clock[0]
print '%s finished. Duration %.6f seconds.' % (op, duration)
clock[0] = time.time()
# Example
if __name__ == '__main__':
import array
dur() # Initialise the timing clock
opt1 = array.array('H')
for i in range(1000):
for n in range(1000):
opt1.append(n)
dur('Array from append')
opt2 = array.array('H')
seq = range(1000)
for i in range(1000):
opt2.extend(seq)
dur('Array from list extend')
opt3 = array.array('H')
seq = array.array('H', range(1000))
for i in range(1000):
opt3.extend(seq)
dur('Array from array extend')
# Output:
# Array from append finished. Duration 0.175320 seconds.
# Array from list extend finished. Duration 0.068974 seconds.
# Array from array extend finished. Duration 0.001394 seconds.
定义在默认的计时器中,针对不同平台采用不同方式。在Windows上,time.clock()具有微秒精度,但是time.time()精度是1/60s。在Unix上,time.clock()有1/100s精度,而且time.time()精度远远更高。在另外的平台上,default_timer()测量的是墙上时钟时间,不是CPU时间。这意味着同一计算机的其他进程可能影响计时
版权声明:
def clock(func):
def clocked(*args, **kwargs):
t0 = timeit.default_timer()
result = func(*args, **kwargs)
elapsed = timeit.default_timer() - t0
name = func.__name__
arg_str = ', '.join(repr(arg) for arg in args)
print('[%0.8fs] %s(%s) - %r' % (elapsed, name, arg_str, result))
return result
return clocked
@clock
def run(seconds):
time.sleep(seconds)
return time
if __name__ == '__main__':
run(1)
本文为CSDN博主「FlyingPie」的原创文章,遵循CC 4.0 BY-SA版权协议,附上原文出处链接及声明。
原文链接:
参考资料:CSDN。