0

I create a decorator to time the cost of every funcions :

def timmer(func):
  def wrapper(*args, **kwargs):
      start_time = time.time()
      res = func(*args, **kwargs)
      stop_time = time.time()
      print('Func %s, run time: %s' % (func.__name__, stop_time - start_time))
      return res
return wrapper

And use it every def , for example :

@timmer
def Test_func(*args,**kwargs):
   return "hello !"

Then I can obtain the cost time of every func.

But if I do this , I have to use @timmer in every def .If this .py file has 100 defs , then I must use @timmer for 100 times.

How can I get the running time of each method conveniently ?

UNA
  • 1

1 Answers1

0
  1. Create a master function which in turn calls any one of the 100 functions with the function name as a parameter
  2. Use the decorator with this master function
  3. Call the master function in a loop for all the 100 functions and save results in a list.append()
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51