reperf.py 538 B

1234567891011121314151617181920212223
  1. import re
  2. import time
  3. def main():
  4. s = "\13hello\14 \13world\14 " * 1000
  5. p = re.compile(r"([\13\14])")
  6. timefunc(10, p.sub, "", s)
  7. timefunc(10, p.split, s)
  8. timefunc(10, p.findall, s)
  9. def timefunc(n, func, *args, **kw):
  10. t0 = time.perf_counter()
  11. try:
  12. for i in range(n):
  13. result = func(*args, **kw)
  14. return result
  15. finally:
  16. t1 = time.perf_counter()
  17. if n > 1:
  18. print(n, "times", end=' ')
  19. print(func.__name__, "%.3f" % (t1-t0), "CPU seconds")
  20. main()