You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

24 lines
596 B

  1. # -*- coding: utf-8 -*-
  2. # Simple timer to time a block of code, for optimization debugging
  3. # use like:
  4. # with nilmdb.utils.Timer("flush"):
  5. # foo.flush()
  6. from __future__ import print_function
  7. from __future__ import absolute_import
  8. import contextlib
  9. import time
  10. @contextlib.contextmanager
  11. def Timer(name = None, tosyslog = False):
  12. start = time.time()
  13. yield
  14. elapsed = int((time.time() - start) * 1000)
  15. msg = (name or 'elapsed') + ": " + str(elapsed) + " ms"
  16. if tosyslog: # pragma: no cover
  17. import syslog
  18. syslog.syslog(msg)
  19. else:
  20. print(msg)