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)

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

5 Comments

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()
Can you clarify what you mean by running your function in an infinite loop. A truly infinite loop would crash your program.
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
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 that should do it for you
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

Comments

0

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

2 Comments

Will work this link when I use a loop for call of function? I have tried this but it raise exception and it stops after first call.
can you tell me the exception you're getting?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.