def _profile(filename, fn, *args, **kw): gc.collect() profiler.runctx('result = fn(*args, **kw)', globals(), locals(), filename=filename) return locals()['result']