我正在創建一個代碼,要求程序計算其運行時間,然後顯示時間。它基本上是一個在後臺運行的計時器,我可以調用它來顯示代碼已經運行了多長時間。我該怎麼做呢?如何在python上創建計時器
3
A
回答
13
您記錄開始時間,然後再計算開始時間和當前時間之間的差異。
到由於平臺的差異,精密您要使用的timeit.default_timer
callable:
from timeit import default_timer
start = default_timer()
# do stuff
duration = default_timer() - start
這讓你在幾秒鐘內掛鐘時間持續時間爲浮動點值。
演示:
>>> from timeit import default_timer
>>> start = default_timer()
>>> # Martijn reads another post somewhere
...
>>> print default_timer() - start
19.1996181011
4
這是很容易在Python
import time
start_time=time.time()
#do something
end_time=time.time()-start_time
所得end_time
會在幾秒鐘內
0
我已經這樣做了,這可能會幫助你
from timeit import default_timer
timerPool = {}
TIMER_RUNNING = 1
TIMER_STOPPED = 0
TIMER_IDLE = 2
"""
Initilialize the timer like below if any new timer to be added
"""
"""
required initialization for "eightSecTimer_Id" timer
"""
timerPool['eightSecTimer_Id'] = {}
timerPool['eightSecTimer_Id']['state'] = TIMER_IDLE
timerPool['eightSecTimer_Id']['start'] = 0
timerPool['eightSecTimer_Id']['duration'] = 0
timerPool['eightSecTimer_Id']['time'] = 0
"""
required initialization for "fiveSecTimer_Id" timer
"""
timerPool['fiveSecTimer_Id'] = {}
timerPool['fiveSecTimer_Id']['state'] = TIMER_IDLE
timerPool['fiveSecTimer_Id']['start'] = 0
timerPool['fiveSecTimer_Id']['duration'] = 0
timerPool['fiveSecTimer_Id']['time'] = 0
"""
Interface to start the timer
"""
def StartTimer(Id,time):
timerPool[Id]['time'] = time
timerPool[Id]
if (timerPool[Id]['state'] == TIMER_IDLE) or (timerPool[Id]['state'] == TIMER_STOPPED):
timerPool[Id]['start'] = default_timer()
timerPool[Id]['state'] = TIMER_RUNNING
return timerPool[Id]['state']
"""
Interface to get the timer status.
Return "TIMER_STOPPED" when timer completed
Return "TIMER_IDLE" after timer completed on consecutive call of this function
"""
def GetTimerState(Id):
time = timerPool[Id]['time']
if timerPool[Id]['state'] == TIMER_RUNNING:
timerPool[Id]['duration'] = default_timer() - timerPool[Id]['start']
else:
None
if timerPool[Id]['state'] == TIMER_STOPPED:
timerPool[Id]['state'] = TIMER_IDLE
if timerPool[Id]['duration'] >= time:
timerPool[Id]['state'] = TIMER_STOPPED
timerPool[Id]['duration'] = 0
return timerPool[Id]['state']
"""
Below is how to use.
"""
StartTimer('fiveSecTimer_Id',5)
StartTimer('eightSecTimer_Id',8)
while True:
if GetTimerState('fiveSecTimer_Id') == TIMER_STOPPED:
print "5 sec Timer Stopped"
if GetTimerState('eightSecTimer_Id') == TIMER_STOPPED:
print "8 sec Timer Stopped"
sleep (.5)
+0
而不是嵌套字典,爲什麼不使用自定義類?它會使代碼更具可讀性。 –
相關問題
- 1. 如何創建計時器?
- 2. 如何在Cookie中創建計時器?
- 3. 如何在C#中創建計時器?
- 4. 如何在Python中創建計時器循環cv.PutText
- 5. 無法在Jboss5上創建計時器
- 6. 如何使用JavaScript在視頻上創建計時器?
- 7. 如何在netlogo上創建一個計時器?
- 8. 如何在C#中創建一個計時器計數器
- 9. 如何創建秒錶計時器
- 10. 如何創建PowerPoint計時器?
- 11. 如何創建日期計時器
- 12. 如何使用JLabel創建計時器?
- 13. 如何創建計數器?
- 14. 如何在python中建立計數器?
- 15. 如何在C#中創建一個定時器/計數器XNA
- 16. 如何在Python中創建分數計數器?
- 17. 如何創建報表設計器在Python
- 18. 如何在Python中創建高爾夫Stableford計算器
- 19. 如何在計時器達到零時創建動作
- 20. 級別計時器 - 如何在Unity中創建時間限制?
- 21. 如何在python上創建.ASPXAUTH cookie
- 22. 如何在Github上創建Python Django WebApplication?
- 23. 如何在Python 3.5.2上創建表?
- 24. 如何在Python 2上創建窗口?
- 25. 如何在Python上創建類?
- 26. 創建GUI計算器在python類似於MS計算器
- 27. 計時器正在創建多個計時器實例
- 28. 需要幫助創建一個計時器在Python
- 29. 如何使計時器在Python中實時計時?
- 30. 在php中創建計時器算法
Th對你的答案非常有幫助。 –