timeit_truncate.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python3
  2. """
  3. Time the execution of the truncate functions
  4. """
  5. import timeit
  6. import sys
  7. from truncate import (
  8. truncate_funcs,
  9. truncate_by_concating,
  10. truncate_by_backing_up_bytes,
  11. )
  12. TESTSTRS = {
  13. 'SHORT_ASCII': 'Dog',
  14. 'LONG_ASCII': 'The quick brown fox jumps over the lazy dog.',
  15. 'SHORT_UNICODE': '\U0001F600\u2014\xD6',
  16. 'LONG_UNICODE': 'The\U0001F600quick\u2014brown\xD6fox\U0001F600jumps\u2014over\xD6the\U0001F600lazy\u2014dog.',
  17. }
  18. TIMEIT_SETUP = 'import timeit_truncate as tt'
  19. def time_executions_A(uncut=False):
  20. """
  21. Time executions via timeit module level calls
  22. """
  23. for strname in ['SHORT_UNICODE', 'LONG_UNICODE']: # only unicode strings
  24. for truncate in truncate_funcs:
  25. cut_txt = "UNCUT" if uncut else "CUT at len-2"
  26. cut_len = sys.maxsize if uncut else len(TESTSTRS[strname]) - 2
  27. print(f"Time '{truncate.__name__}' with {strname} string {cut_txt}")
  28. stmt = (
  29. f"tt.{truncate.__name__}(tt.TESTSTRS['{strname}'], {cut_len})"
  30. )
  31. timeit.main(
  32. ['-u', 'usec', '-n', '100000', '-s', TIMEIT_SETUP, stmt]
  33. )
  34. if __name__ == '__main__':
  35. time_executions = time_executions_A
  36. print("--- Timeings WITHOUT cutting the strings ---")
  37. time_executions(uncut=True)
  38. print("\n--- Timeings WITH cutting the strings (at len-2) ---")
  39. time_executions(uncut=False)