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 779 B

1234567891011121314151617181920212223242526272829
  1. import time
  2. from contextlib import contextmanager
  3. @contextmanager
  4. def check_time_elapse(seconds:float, op='lt'):
  5. """
  6. 检测某一段程序所花费的时间,是否 op 给定的seconds
  7. :param 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, (end-start, seconds)
  16. elif op == 'gt':
  17. assert end-start > seconds, (end-start, seconds)
  18. elif op == 'eq':
  19. assert end - start == seconds, (end-start, seconds)
  20. elif op == 'le':
  21. assert end - start <= seconds, (end-start, seconds)
  22. elif op == 'ge':
  23. assert end - start >= seconds, (end-start, seconds)
  24. else:
  25. raise ValueError("Only supports lt,gt,eq,le,ge.")