重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
当range函数里面的参数为单个变量时,这个变量#代表是长度 为6就是要从0开始数到某个元素为止,这些元素的个数一共是6个,如果按照c语言的话可能是这么写的for(i=0 ;i6;i++)。写range(6)和range(0,5)没有区别。
太和ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
用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.
用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.
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对列表计时的方法:
使用“import”语句导入time包,在列表操作之前用time.time函数获取当前时间,在列表操作之后,再用time.time获取当前时间,用第二次的时间减去第一次的时间就可以了
示例如下:
执行结果如下:
更多Python知识,请关注:Python自学网!!