Created
March 21, 2017 15:59
-
-
Save ssoto/010f1c853fb0c644a0c4e501e4a12d3c to your computer and use it in GitHub Desktop.
Python time profiling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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