0

If I have a function called a lot of times in a for loop and this function sometimes is running too much time, how can I use a timer for each call of function(to set and reset the timer each time)?

It looks like:

def theFunction(*args):
     #some code (timer is on)
     #In this point time is out, break and exit function
     #Timer is reseted
for i in range(0,100):  
     theFunction(*args)
mgracer
  • 175
  • 1
  • 11
Developper
  • 89
  • 1
  • 10

3 Answers3

2

Use the time module like so:

import time

time_start = time.time()
#Do function stuff
time_stop = time.time()
#Check your time now
timed_segment = time_stop - time_start
#Repeat if needed

To run this multiple times in a for loop you will need to append times into a list as it runs like so:

import time

def function():
    times_list = []
    for x in range(10)
        time_start = time.time()
        #Do function stuff
        time_stop = time.time()
        #Check your time now
        timed_segment = time_stop - time_start
        times_list.append(timed_segment)
        #Repeat as many times as needed
    return times_list

If you want to break after a certain amount of time you can use a while loop instead like so:

import time

def function():
    times_list = []
    time_start = time.time()
    time_end = time.time()
    while time_end - time_start < 10: #after 10 seconds the while loop will time out
        #Your function does stuff here
        time_end = time.time()
        #Next, append times to a list if needed
        time_list.append(time_start - time_end)
    return times_list

To stop the function after a certain time regardless of where it is, we can use threading like so:

import threading
from time import sleep

def do_stuff():
    sleep(10)
    print("1 + 2")
    return

t = threading.Thread(target=do_stuff)
t.start()
t.join(timeout = 5)

In the above example, calling timeout in join will kill the thread after 5 seconds. We can also put this into a decorator if we plan on reusing it many times like so:

import threading
from time import sleep

def timeout(func):
    def inner_func(*nums, **kwargs):
        t = threading.Thread(target=func, args=(*nums,))
        t.start()
        t.join(timeout=5)
    return inner_func

@timeout
def do_stuff(a,b):
    sleep(3)
    print(a+b)
    return

do_stuff(1,3)
Matt
  • 1,368
  • 1
  • 26
  • 54
  • Will this work if my function run with a infinite loop fo example? I mean: Time_start=time.time() || Infinite Loop // I need to break this loop after 10 seconds || Time_Stop=time.time() – Developper Oct 03 '18 at 19:21
  • Can you clarify what you mean by running your function in an infinite loop. A truly infinite loop would crash your program. – Matt Oct 03 '18 at 21:03
  • I've added a chunk of code onto the bottom of my answer to address what it seems you are looking for. Let me know if that helps – Matt Oct 03 '18 at 21:09
  • Ok, my question for this thing is one, I set my timer and doing function stuff, my function stuff will durate 10 minutes for exemple, and then we will get time_end. But I don't want to wait 10 minutes for my funcion stuff, In the 10s second of executing my stuff I want to break imediatly the funcion. Yea... That's my problem I know that I need a decorator, but I don't know how to use it in a function call loop, Where I need to stop and restart it... – Developper Oct 04 '18 at 09:20
  • @Developper that should do it for you – Matt Oct 05 '18 at 15:29
1

There is another module called timeit which can measure the execution time of small code snippets. I believe you can use that also. I have never used that module but it should work.

Here is the link to the doc page. Give it a look :: https://docs.python.org/2/library/timeit.html

see How to use timeit module as well

Marc Wagner
  • 1,672
  • 2
  • 12
  • 15
Dibakar Bose
  • 100
  • 1
  • 8
0

For high re-usability and ease of implementations, I would recommend -

Saransh Gupta
  • 63
  • 1
  • 10