導航:首頁 > 編程語言 > python實現計時功能

python實現計時功能

發布時間:2022-08-04 22:23:03

python 怎麼計時

import time
s = 0
while(True):
time.sleep(1)
s = s+1
print(s)
--------------------
新手獻丑

㈡ 用python 計時器怎麼做,

用python實現計時器功能,代碼如下:

'''SimpleTimingFunction.

previouscall.ItworkswithmostPython2.xplatforms.Thefunction
(clock)without
usingaglobalvariable.
'''
importtime
defr(op=None,clock=[time.time()]):
ifop!=None:
ration=time.time()-clock[0]
print'%sfinished.Duration%.6fseconds.'%(op,ration)
clock[0]=time.time()
#Example
if__name__=='__main__':
importarray
r()#Initialisethetimingclock
opt1=array.array('H')
foriinrange(1000):
forninrange(1000):
opt1.append(n)
r('Arrayfromappend')
opt2=array.array('H')
seq=range(1000)
foriinrange(1000):
opt2.extend(seq)
r('Arrayfromlistextend')
opt3=array.array('H')
seq=array.array('H',range(1000))
foriinrange(1000):
opt3.extend(seq)
r('Arrayfromarrayextend')
#Output:
#Arrayfromappendfinished.Duration0.175320seconds.
#Arrayfromlistextendfinished.Duration0.068974seconds.
#Arrayfromarrayextendfinished.Duration0.001394seconds.

㈢ python的計時器

你可以用Twisted來實現,源代碼如下:

from twisted.internet import task
from twisted.internet import reactor

#set global variables
g = 0
def run():
global g
g = g + 1
print 'the global value g is:%s'%g
#add function run to twisted's looping call
l = task.LoopingCall(run)
#set interval to 5*60 seconds
l.start(5*60)
reactor.run()
要運行這段代碼你得裝twisted和zope中關於interface的定義,上google搜搜載一個裝了就可以了。

㈣ Python2.7.13怎麼編計時器

用python實現計時器功能,代碼如下:

''SimpleTimingFunction.

previouscall.ItworkswithmostPython2.xplatforms.Thefunction
(clock)without
usingaglobalvariable.
'''
importtime
defr(op=None,clock=[time.time()]):
ifop!=None:
ration=time.time()-clock[0]
print'%sfinished.Duration%.6fseconds.'%(op,ration)
clock[0]=time.time()
#Example
if__name__=='__main__':
importarray
r()#Initialisethetimingclock
opt1=array.array('H')
foriinrange(1000):
forninrange(1000):
opt1.append(n)
r('Arrayfromappend')
opt2=array.array('H')
seq=range(1000)
foriinrange(1000):
opt2.extend(seq)
r('Arrayfromlistextend')
opt3=array.array('H')
seq=array.array('H',range(1000))
foriinrange(1000):
opt3.extend(seq)
r('Arrayfromarrayextend')
#Output:
#Arrayfromappendfinished.Duration0.175320seconds.
#Arrayfromlistextendfinished.Duration0.068974seconds.
#Arrayfromarrayextendfinished.Duration0.001394seconds.

題主空閑的時候可以多看看Python的相關教程,黑馬程序員再往上有許多免費的教程,想學習的可以下載下來多看看,多學習學習,以後類似的問題就可以迎刃而解了。https://..com/question/1051689931722045579.html?fr=android_app&share_time=1499421328373

㈤ python怎麼寫計時器用面向對象和多向進程

timeit

㈥ 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=' ')
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), '秒', '...暫停中 按回車繼續,輸入Q停止計時',end=' ') 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

㈦ 使用Python,實現程序運行計時的數碼管表示

用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 r( op=None, clock=[time.time()] ):
if op != None:
ration = time.time() - clock[0]
print '%s finished. Duration %.6f seconds.' % (op, ration)
clock[0] = time.time()
# Example
if __name__ == '__main__':
import array
r() # Initialise the timing clock
opt1 = array.array('H')
for i in range(1000):
for n in range(1000):
opt1.append(n)
r('Array from append')
opt2 = array.array('H')
seq = range(1000)
for i in range(1000):
opt2.extend(seq)
r('Array from list extend')
opt3 = array.array('H')
seq = array.array('H', range(1000))
for i in range(1000):
opt3.extend(seq)
r('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 如何添加計時器

使用time模塊
程序最開始的時候使用time.time()得到一個時間
然後,再需要查看時間的地方調用一下time.time(),得到另一個時間,然後兩個時間相減就可以得到程序執行的時間,得到的是秒數,可以自己算一下得到分鍾這類的

閱讀全文

與python實現計時功能相關的資料

熱點內容
自己建了伺服器地址 瀏覽:696
命令按鈕的屬性設置 瀏覽:961
證券技術分析pdf 瀏覽:777
linux命令連接oracle 瀏覽:200
墊江停車收費樁怎麼上App 瀏覽:133
好興動app還款怎麼登錄不上去了 瀏覽:665
鄭州雲伺服器託管 瀏覽:722
伺服器地址跟蹤 瀏覽:980
免費google雲伺服器 瀏覽:516
摘譯和編譯的英文 瀏覽:359
熱泵壓縮機選型 瀏覽:121
op手機微信加密如何解除 瀏覽:386
如何在王牌戰爭找到高爆率伺服器 瀏覽:13
江浙小學語文輔導課用什麼APP 瀏覽:99
新夢幻大陸伺服器地址 瀏覽:241
網吧伺服器怎麼更換壁紙 瀏覽:530
linux命令方法 瀏覽:332
linux下載freetype 瀏覽:123
程序員入駐平台 瀏覽:327
程序員大戰外掛 瀏覽:745