Skip to content

Instantly share code, notes, and snippets.

@monsterxx03
Created March 19, 2014 09:22
Show Gist options
  • Save monsterxx03/9638314 to your computer and use it in GitHub Desktop.
Save monsterxx03/9638314 to your computer and use it in GitHub Desktop.
cprofile
def profile(result_file_name):
def wrapper(fn):
def inner_wrapper(*args, **kwargs):
import cProfile, pstats
pr = cProfile.Profile()
pr.enable()
value = fn(*args, **kwargs)
pr.disable()
ps = pstats.Stats(pr)
ps.dump_stats(result_file_name)
return value
return inner_wrapper
return wrapper
from contextlib import contextmanager
@contextmanager
def profile(result_file_name):
import cProfile, pstats
pr = cProfile.Profile()
pr.enable()
try:
yield
finally:
pr.disable()
ps = pstats.Stats(pr)
ps.dump_stats(result_file_name)
with profile('/nova/yejia/nova_api.stats'):
xxx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment