Home > Blockchain >  Measuring Function runtime with waiting function
Measuring Function runtime with waiting function

Time:10-22

Someone gave me this code for a timing measurement decorator for functions and it works perfectly fine:

from functools import wraps
from time import time

def timing(f):
    @wraps(f)
    def wrap(*args, **kw):
        ts = time()
        result = f(*args, **kw)
        te = time()
        print(f'func:{f.__name__} args:[{args}, {kw}] took: {te-ts} sec')
        return result
    return wrap

@timing
def myfunc(count):
    for _ in range(count):
        print("Hello World")

I just tried it on another function that waits for the user to close a popup window before it finishes. THe. problem is that the timer now measures the time the window was open (and idle) and not just the actual time it took. How do I bypass this problem?

Thanks in advance!

CodePudding user response:

You may want to define an inner function which is called only after the user has closed the window, and decorate that one:

def outer_func(arg):
    @timing
    def inner_func(arg):
        #do the actual stuff
    #wait for the user to close the window
    inner_func(arg)
  • Related