Skip to content

Instantly share code, notes, and snippets.

@ssoto
Created March 21, 2017 15:59
Show Gist options
  • Save ssoto/010f1c853fb0c644a0c4e501e4a12d3c to your computer and use it in GitHub Desktop.
Save ssoto/010f1c853fb0c644a0c4e501e4a12d3c to your computer and use it in GitHub Desktop.
Python time profiling
#!python
# -*- coding: utf-8 -*-
import importlib
import os
from timeit import default_timer as timer
import logging
from my_module import config
class benchmark(object):
logger = config.getLogger()
def __init__(self, msg, fmt="%0.3g"):
self.msg = msg
self.fmt = fmt
def __enter__(self):
self.start = timer()
return self
def __exit__(self, *args):
t = timer() - self.start
logger.info(("%s : " + self.fmt + " seconds") % (self.msg, t))
self.time = t
if __name__ == '__main__':
with benchmark('time executing my function') as b:
foo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment