How to create a timer to tell how long the code has been running
Asked Answered
F

4

6

I am creating code that requires the program to time how long it runs for and then displays the time. It basically needs a timer that runs in the background and I can call upon it to display how long the code has been running for. How do I do this?

Fictitious answered 20/6, 2013 at 10:22 Comment(0)
B
22

You record the start time, then later on calculate the difference between that start time and the current time.

Due to platform differences, for precision you want to use the timeit.default_timer callable:

from timeit import default_timer

start = default_timer()

# do stuff

duration = default_timer() - start

This gives you a wall-clock time duration in seconds as a floating point value.

Demo:

>>> from timeit import default_timer
>>> start = default_timer()
>>> # Martijn reads another post somewhere
... 
>>> print default_timer() - start
19.1996181011
Becalm answered 20/6, 2013 at 10:23 Comment(0)
R
6

This is easy in python

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

The resultant end_time will be in seconds

Ra answered 20/6, 2013 at 10:28 Comment(0)
M
-1

I have done this, This may help you

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)
Mystical answered 29/3, 2014 at 7:15 Comment(2)
Instead of nested dictionaries, why not use custom classes? It'd make the code a lot more readable.Becalm
What could possibly be the purpose of this code?Poland
I
-1
    from threading import Timer
    
    def repeater(interval, function):
        Timer(interval, repeater, [interval, function]).start()
        function()
    
    # Example usage: function `my_task` will be called every 2 seconds.
    def my_task():
        print("Task accomplished")

repeater(2, my_task)
Illicit answered 14/10 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.