Skip to content

Instantly share code, notes, and snippets.

@terryjbates
Created August 1, 2016 22:37
Show Gist options
  • Save terryjbates/6c07b0f99f5d77c19068b004b962b5ef to your computer and use it in GitHub Desktop.
Save terryjbates/6c07b0f99f5d77c19068b004b962b5ef to your computer and use it in GitHub Desktop.
########################## timer.py
# Import timings
import time
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args):
self.end = time.time()
self.secs = self.end - self.start
self.msecs = self.secs * 1000 # millisecs
if self.verbose:
print('elapsed time: %f ms' % self.msecs)
def timing_decorator(func):
def func_wrapper(*args, **kwargs):
with Timer() as t:
query_results = func(*args, **kwargs)
print('Elapsed time to process query: {0}'.format(t.secs))
return query_results
return func_wrapper
##################### sample.py
#!/usr/bin/env python
import sys
import os
from timer import timing_decorator as timing_deco
import logging
log_level = logging.INFO
logging.basicConfig(level=log_level, format='[%(levelname)s] %(message)s')
log = logging.getLogger(__name__)
def timing_decorator(func):
def func_wrapper(*args, **kwargs):
with timer.Timer() as t:
query_results = func(*args, **kwargs)
print('Elapsed time to process query: {0}'.format(t.secs))
return query_results
return func_wrapper
@timing_deco
def main():
''' Execute your code here.'''
print("foobar")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment