Advertisement
surik00

Replace symbols with regex or with replace

Oct 18th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. import re
  2. import timeit
  3.  
  4. regex = re.compile(r'[.,?!\|/@]')
  5. line = 'q@wertysiodhaorhgsdfgsdf/g|sdhgae.,asrg,asrgargarg,.aer.g,aerg!qmfpoibhsdfoinjbosinr?!..,'
  6.  
  7.  
  8. def with_regex():
  9.     my_line = line
  10.     my_line = regex.sub('', my_line)
  11.  
  12.  
  13. def with_replace():
  14.     my_line = line
  15.     for c in '.,?!\\|/@':
  16.         my_line = my_line.replace(c, '')
  17.  
  18.  
  19. timeit.timeit(with_regex)
  20. timeit.timeit(with_replace)
  21.  
  22. '''
  23. >>> timeit.timeit(with_regex)
  24. 3.323782834816426
  25. >>> timeit.timeit(with_replace)
  26. 2.9493549943281825
  27. '''
  28.  
  29. # 2 ################################################################################
  30.  
  31. import re
  32. import timeit
  33.  
  34. regex = re.compile(r'[.,?!\|/@]')
  35. line = 'q@wertysiodhaorhgsdfgsdf/g|sdhgae.,asrg,asrgargarg,.aer.g,aerg!qmfpoibhsdfoinjbosinr?!..,'
  36. line = line * 1000
  37.  
  38.  
  39. def with_regex():
  40.     my_line = line
  41.     my_line = regex.sub('', my_line)
  42.  
  43.  
  44. def with_replace():
  45.     my_line = line
  46.     for c in '.,?!\\|/@':
  47.         my_line = my_line.replace(c, '')
  48.  
  49.  
  50. timeit.timeit(with_regex, number=5000)
  51. timeit.timeit(with_replace, number=5000)
  52.  
  53. '''
  54. >>> timeit.timeit(with_regex, number=5000)
  55. 14.224216839779407
  56. >>> timeit.timeit(with_replace, number=5000)
  57. 5.4049051833698005
  58. >>>
  59. '''
  60.  
  61. # 3 ################################################################################
  62.  
  63. import re
  64. import timeit
  65.  
  66. regex = re.compile(r'[.,?!\|/@]')
  67. line = 'q@wertysiodhaorhgsdfgsdf/g|sdhgae.,asrg,asrgargarg,.aer.g,aerg!qmfpoibhsdfoinjbosinr?!..,'
  68. line = line * 1000
  69.  
  70.  
  71. def with_regex_wrapper(_line):
  72.     def with_regex():
  73.         my_line = _line
  74.         my_line = regex.sub('', my_line)
  75.         return my_line
  76.     return with_regex
  77.  
  78.  
  79. def with_replace_wrapper(_line):
  80.     def with_replace():
  81.         my_line = _line
  82.         for c in '.,?!\\|/@':
  83.             my_line = my_line.replace(c, '')
  84.         return my_line
  85.     return with_replace
  86.  
  87.  
  88. timeit.timeit(with_regex_wrapper(line), number=5000)
  89. timeit.timeit(with_replace_wrapper(line), number=5000)
  90.  
  91.  
  92. '''
  93. >>> timeit.timeit(with_regex_wrapper(line), number=5000)
  94. 14.298113452276521
  95. >>> timeit.timeit(with_replace_wrapper(line), number=5000)
  96. 5.418057598813073
  97. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement