Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python上创建计时器_Python - Fatal编程技术网

如何在python上创建计时器

如何在python上创建计时器,python,Python,我正在创建一个代码,要求程序计算它运行的时间,然后显示时间。它基本上是一个在后台运行的计时器,我可以调用它来显示代码已经运行了多长时间。我该怎么做 记录开始时间,然后计算该开始时间与当前时间之间的差值 由于平台的差异,为了达到精度,您需要使用: 这将为您提供以秒为单位的挂钟持续时间作为浮点值 演示: 这在python中很容易 import time start_time=time.time() #do something end_time=time.time()-start_time 生成的结

我正在创建一个代码,要求程序计算它运行的时间,然后显示时间。它基本上是一个在后台运行的计时器,我可以调用它来显示代码已经运行了多长时间。我该怎么做

记录开始时间,然后计算该开始时间与当前时间之间的差值

由于平台的差异,为了达到精度,您需要使用:

这将为您提供以秒为单位的挂钟持续时间作为浮点值

演示:

这在python中很容易

import time
start_time=time.time()
#do something
end_time=time.time()-start_time

生成的
结束时间
将以秒为单位

我已经这样做了,这可能会对您有所帮助

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)

为什么不使用自定义类而不是嵌套字典呢?这将使代码更具可读性。
import time
start_time=time.time()
#do something
end_time=time.time()-start_time
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)