0

Given be a program that has many functions, how can I find out how much time spends in every functions?

For example it is very simple to track the execution time of one funtion, 27 possible solutions are laid out in this post, here.

However what would be a sensible extension to an already exisiting project to keep track on the times of all its functions during one execution? The total run time might be a few hours, thus it might be very interesting/revealing to know, if there are spots, that need improvements.

Imago
  • 521
  • 6
  • 29

2 Answers2

2

If you have not yet tried to use the built-in profiler, see: https://docs.python.org/3/library/profile.html

If you have, and that is not doing what you need, you can use a decorator on each function to report the time spent in that function. A really good example of this is at: https://codereview.stackexchange.com/questions/169870/decorator-to-measure-execution-time-of-a-function

user1827408
  • 21
  • 1
  • 4
1

Try to add a decorator to each function to check the execution time for each one:

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 'func:%r args:[%r, %r] took: %2.4f sec' % \
          (f.__name__, args, kw, te-ts)
        return result
    return wrap

@timing
your_func()
jcf
  • 602
  • 1
  • 6
  • 26