python對(duì)列表計(jì)時(shí)的方法:
創(chuàng)新互聯(lián)公司專(zhuān)注于桐梓網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供桐梓營(yíng)銷(xiāo)型網(wǎng)站建設(shè),桐梓網(wǎng)站制作、桐梓網(wǎng)頁(yè)設(shè)計(jì)、桐梓網(wǎng)站官網(wǎng)定制、微信小程序開(kāi)發(fā)服務(wù),打造桐梓網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供桐梓網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
使用“import”語(yǔ)句導(dǎo)入time包,在列表操作之前用time.time函數(shù)獲取當(dāng)前時(shí)間,在列表操作之后,再用time.time獲取當(dāng)前時(shí)間,用第二次的時(shí)間減去第一次的時(shí)間就可以了
示例如下:
執(zhí)行結(jié)果如下:
更多Python知識(shí),請(qǐng)關(guān)注:Python自學(xué)網(wǎng)?。?/p>
import time
print('按下回車(chē)開(kāi)始計(jì)時(shí),按下 Ctrl + C 暫停/停止計(jì)時(shí)。')
while True:
input("")
starttime = time.time()
print('開(kāi)始')
pausetime=0
while True:
try:
totaltime=int((time.time()-pausetime-starttime))
print('【'+str(int((time.time()-pausetime-starttime)*0.66)),'進(jìn)度】',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)), '進(jìn)度】', int((time.time()-pausetime - starttime) / 60), '分',
int((time.time() -pausetime- starttime) % 60), '秒', '...暫停中\(zhòng)r\n按回車(chē)?yán)^續(xù),輸入Q停止計(jì)時(shí)',end='\r')? ? ? ? ? ? ?pausetime_start=time.time()
s=input("")
if s.strip().upper()=='Q':
pausetime_end = time.time()
pausetime = pausetime_end - pausetime_start + pausetime
print('結(jié)束')
endtime = time.time()
print('總時(shí)間:', 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實(shí)現(xiàn)計(jì)時(shí)器功能,代碼如下:
''' 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.
定義在默認(rèn)的計(jì)時(shí)器中,針對(duì)不同平臺(tái)采用不同方式。在Windows上,time.clock()具有微秒精度,但是time.time()精度是1/60s。在Unix上,time.clock()有1/100s精度,而且time.time()精度遠(yuǎn)遠(yuǎn)更高。在另外的平臺(tái)上,default_timer()測(cè)量的是墻上時(shí)鐘時(shí)間,不是CPU時(shí)間。這意味著同一計(jì)算機(jī)的其他進(jìn)程可能影響計(jì)時(shí)
版權(quán)聲明:
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」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,附上原文出處鏈接及聲明。
原文鏈接:
參考資料:CSDN。
當(dāng)前名稱(chēng):python函數(shù)內(nèi)計(jì)時(shí)的簡(jiǎn)單介紹
本文路徑:http://aaarwkj.com/article2/dsiijic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)站策劃、小程序開(kāi)發(fā)、品牌網(wǎng)站設(shè)計(jì)、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)