導航:首頁 > 編程語言 > 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實現計時功能相關的資料

熱點內容
php獨立運行 瀏覽:530
手機sh執行命令 瀏覽:727
雲伺服器的角色 瀏覽:733
單片機頻率比例 瀏覽:840
我的世界伺服器如何關閉正版驗證 瀏覽:504
如何查roid伺服器上的 瀏覽:130
安卓手機主板如何撬晶元不掉電 瀏覽:249
php各個框架的優缺點 瀏覽:101
php1100生成數組 瀏覽:359
以後做平面設計好還是程序員好 瀏覽:552
雲伺服器應用管理 瀏覽:438
飢荒雲伺服器搭建過程 瀏覽:186
可編程式控制制器優點 瀏覽:99
壓縮垃圾車說明書 瀏覽:28
五輪書pdf 瀏覽:802
單片機定時流水中斷系統流水燈 瀏覽:701
u8如何連接伺服器配置 瀏覽:68
動力在於緩解壓力 瀏覽:867
報考科一用什麼app 瀏覽:346
knn人臉識別演算法 瀏覽:431