1. 如何用python寫一個計時器
可以用time.sleep(1)睡眠一秒加一,然後大於180秒是停止,用while會方便一點,
望採納
2. 用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.
3. python怎麼寫計時器用面向對象和多向進程
timeit
通常在一段程序的前後都用上time.time(),然後進行相減就可以得到一段程序的運行時間,不過python提供了更強大的計時庫:timeit
測試一個函數的執行時間:
此程序測試函數運行1000次的執行時間
repeat:
由於電腦永遠都有其他程序也在佔用著資源,你的程序不可能最高效的執行。所以一般都會進行多次試驗,取最少的執行時間為真正的執行時間。
4. 關於Python里計時器的問題
HIWORD =''
def hi():
a = raw_input ("")
if a=="hi" :
HIWORD=True
hi()
while HIWORD==True:
print "hi"
5. 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
6. 關於python計時器的問題
當range函數裡面的參數為單個變數時,這個變數#代表是長度
為6就是要從0開始數到某個元素為止,這些元素的個數一共是6個,如果按照c語言的話可能是這么寫的for(i=0
;i<6;i++)。寫range(6)和range(0,5)沒有區別。
7. 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搜搜載一個裝了就可以了。
8. 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
9. 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.
10. python關於計時器的問題
當range函數裡面的參數為單個變數時,這個變數#代表是長度 為6就是要從0開始數到某個元素為止,這些元素的個數一共是6個,如果按照c語言的話可能是這么寫的for(i=0 ;i<6;i++)。寫range(6)和range(0,5)沒有區別。