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

utils.py 671 B

123456789101112131415161718192021222324252627282930313233
  1. import time
  2. from contextlib import contextmanager
  3. @contextmanager
  4. def check_time_elapse(seconds, op='lt'):
  5. """
  6. 检测某一段程序所花费的时间,是否 op 给定的seconds
  7. :param int seconds:
  8. :param str op:
  9. :return:
  10. """
  11. start = time.time()
  12. yield
  13. end = time.time()
  14. if op == 'lt':
  15. assert end-start < seconds
  16. elif op == 'gt':
  17. assert end-start > seconds
  18. elif op == 'eq':
  19. assert end - start == seconds
  20. elif op == 'le':
  21. assert end - start <= seconds
  22. elif op == 'ge':
  23. assert end - start >= seconds
  24. else:
  25. raise ValueError("Only supports lt,gt,eq,le,ge.")