24 lines
596 B
Python
24 lines
596 B
Python
# -*- coding: utf-8 -*-
|
|
|
|
# Simple timer to time a block of code, for optimization debugging
|
|
# use like:
|
|
# with nilmdb.utils.Timer("flush"):
|
|
# foo.flush()
|
|
|
|
from __future__ import print_function
|
|
from __future__ import absolute_import
|
|
import contextlib
|
|
import time
|
|
|
|
@contextlib.contextmanager
|
|
def Timer(name = None, tosyslog = False):
|
|
start = time.time()
|
|
yield
|
|
elapsed = int((time.time() - start) * 1000)
|
|
msg = (name or 'elapsed') + ": " + str(elapsed) + " ms"
|
|
if tosyslog: # pragma: no cover
|
|
import syslog
|
|
syslog.syslog(msg)
|
|
else:
|
|
print(msg)
|