Skip to content

Instantly share code, notes, and snippets.

@nvdv
Last active October 4, 2016 09:37
Show Gist options
  • Save nvdv/ddbd08ff8fbef3313cb75241d3115189 to your computer and use it in GitHub Desktop.
Save nvdv/ddbd08ff8fbef3313cb75241d3115189 to your computer and use it in GitHub Desktop.
How profilers work - collecting stats with decorator
def profile_stats(func):
def wrapper(*args, **kwargs):
start_time = time.time()
func_name = func.__name__
# We need this small hack to keep things simple.
caller_name = sys._getframe(1).f_code.co_name
result = func(*args, **kwargs)
stats[func_name][caller_name] += time.time() - start_time
return result
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment