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.
 
 
 

23 lines
557 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. import contextlib
  8. import time
  9. @contextlib.contextmanager
  10. def Timer(name = None, tosyslog = False):
  11. start = time.time()
  12. yield
  13. elapsed = int((time.time() - start) * 1000)
  14. msg = (name or 'elapsed') + ": " + str(elapsed) + " ms"
  15. if tosyslog: # pragma: no cover
  16. import syslog
  17. syslog.syslog(msg)
  18. else:
  19. print(msg)