SHARE
TWEET

Untitled

a guest Jul 15th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # coding=utf-8
  2. import logging
  3. import math
  4. import time
  5. from functools import wraps
  6.  
  7.  
  8. logger = logging.getLogger(__name__)
  9.  
  10.  
  11. class Timer:
  12.     """
  13.     A timing util to measure code execution time in human readable
  14.     format for a snippet of code or method.
  15.     Usage:
  16.     with Timer() as t:
  17.         some code ...
  18.     print(t.runtime)
  19.     or
  20.     @time_it()
  21.     def some_func():
  22.         some code ...
  23.     returns: "time taken by method.module method.name: x seconds x ms"
  24.     for raw timing use raw kwarg
  25.     ex:
  26.     with Timer(raw=True) as t:
  27.         some code ...
  28.     @time_it(raw=True)
  29.     def some_func():
  30.         some code ...
  31.     returns: "time taken by method.module method.name: x.xxxx"
  32.     @dakaugu
  33.     """
  34.  
  35.     def __init__(self, raw=False):
  36.         self.runtime = 0
  37.         self.raw = raw
  38.  
  39.     def __enter__(self):
  40.         self.runtime = self.time_me()
  41.         return self
  42.  
  43.     def __exit__(self, exc_type, exc_val, exc_tb):
  44.         self.runtime = self.time_me(start_time=self.runtime, raw=self.raw)
  45.  
  46.     @staticmethod
  47.     def time_me(start_time=None, raw=False):
  48.         if start_time:
  49.             elapsed_time = time.time() - start_time
  50.             if raw:
  51.                 return elapsed_time
  52.             hours = math.floor(elapsed_time / 3600)
  53.             elapsed_time = elapsed_time - hours * 3600
  54.             minutes = math.floor(elapsed_time / 60)
  55.             elapsed_time = elapsed_time - minutes * 60
  56.             seconds = math.floor(elapsed_time)
  57.             elapsed_time = elapsed_time - seconds
  58.             ms = math.floor(elapsed_time * 1000)
  59.             micros = elapsed_time * 1000 * 1000
  60.             if hours:
  61.                 return "%d hours %d minutes %d seconds" % (hours, minutes, seconds)
  62.             elif minutes:
  63.                 return "%d minutes %d seconds" % (minutes, seconds)
  64.             elif seconds:
  65.                 return "%d seconds %d ms" % (seconds, ms)
  66.             elif ms:
  67.                 return "%d ms" % ms
  68.             return "%d μs" % micros
  69.         return time.time()
  70.  
  71.  
  72. def time_it(raw=False):
  73.     def dec(func):
  74.         @wraps(func)
  75.         def wrapper(*args, **kwargs):
  76.             with Timer(raw=raw) as t:
  77.                 result = func(*args, **kwargs)
  78.             logger.info("time taken by %s %s: %s" %
  79.                         (func.__module__, func.__name__, t.runtime))
  80.             return result
  81.         return wrapper
  82.     return dec
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top