- sys.stdout = open(filename, 'w')
- profile.runctx(function, globals(), locals())
+def profiled(function):
+ @functools.wraps(function)
+ def profiled_function(*args, **kwargs):
+ outfile = open(os.path.join(output_dir, function.__name__), "w")
+ caught = None
+ pr = profile.Profile()
+ pr.enable()
+ try:
+ ret = function(*args, **kwargs)
+ except Exception as e:
+ caught = e
+ pr.disable()
+ ps = pstats.Stats(pr, stream=outfile)
+ ps.sort_stats('time').print_stats()
+ if caught:
+ raise
+ return ret
+ return profiled_function