-1

I have made a program which has like 6-7 functions. I want to see how much time each function takes so that I can improve the one which is consuming a lot of time.

Also, is it possible to calculate cell-wise also?

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

You could do it by creating a decorator function and applying it to the functions of interest.

Here's what I mean:

from functools import wraps
import time

def time_this(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start = time.perf_counter()
        ret = func(*args, **kwargs)
        end = time.perf_counter()
        elapsed = end - start
        print('elapsed time: {}'.format(elapsed))
        return ret
    return wrapper

if __name__ == '__main__':
    # Test it

    @time_this
    def myfunc():
        time.sleep(.25)

    myfunc()  # -> elapsed time: 0.24919553
martineau
  • 119,623
  • 25
  • 170
  • 301