defcumulative_profiler(amount_of_times, sort_stats_by='time'): defreal_decorator(function): defwrapper(*args, **kwargs): nonlocal function, amount_of_times, sort_stats_by # for python 2.x remove this row
profiled_func = _ProfileFunc(function, sort_stats_by) for i inrange(amount_of_times): retval, ps = profiled_func(*args, **kwargs) ps.print_stats() return retval # returns the results of the function return wrapper
ifcallable(amount_of_times): # incase you don't want to specify the amount of times func = amount_of_times # amount_of_times is the function in here amount_of_times = 5# the default amount return real_decorator(func) return real_decorator