Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import timeit
- regex = re.compile(r'[.,?!\|/@]')
- line = 'q@wertysiodhaorhgsdfgsdf/g|sdhgae.,asrg,asrgargarg,.aer.g,aerg!qmfpoibhsdfoinjbosinr?!..,'
- def with_regex():
- my_line = line
- my_line = regex.sub('', my_line)
- def with_replace():
- my_line = line
- for c in '.,?!\\|/@':
- my_line = my_line.replace(c, '')
- timeit.timeit(with_regex)
- timeit.timeit(with_replace)
- '''
- >>> timeit.timeit(with_regex)
- 3.323782834816426
- >>> timeit.timeit(with_replace)
- 2.9493549943281825
- '''
- # 2 ################################################################################
- import re
- import timeit
- regex = re.compile(r'[.,?!\|/@]')
- line = 'q@wertysiodhaorhgsdfgsdf/g|sdhgae.,asrg,asrgargarg,.aer.g,aerg!qmfpoibhsdfoinjbosinr?!..,'
- line = line * 1000
- def with_regex():
- my_line = line
- my_line = regex.sub('', my_line)
- def with_replace():
- my_line = line
- for c in '.,?!\\|/@':
- my_line = my_line.replace(c, '')
- timeit.timeit(with_regex, number=5000)
- timeit.timeit(with_replace, number=5000)
- '''
- >>> timeit.timeit(with_regex, number=5000)
- 14.224216839779407
- >>> timeit.timeit(with_replace, number=5000)
- 5.4049051833698005
- >>>
- '''
- # 3 ################################################################################
- import re
- import timeit
- regex = re.compile(r'[.,?!\|/@]')
- line = 'q@wertysiodhaorhgsdfgsdf/g|sdhgae.,asrg,asrgargarg,.aer.g,aerg!qmfpoibhsdfoinjbosinr?!..,'
- line = line * 1000
- def with_regex_wrapper(_line):
- def with_regex():
- my_line = _line
- my_line = regex.sub('', my_line)
- return my_line
- return with_regex
- def with_replace_wrapper(_line):
- def with_replace():
- my_line = _line
- for c in '.,?!\\|/@':
- my_line = my_line.replace(c, '')
- return my_line
- return with_replace
- timeit.timeit(with_regex_wrapper(line), number=5000)
- timeit.timeit(with_replace_wrapper(line), number=5000)
- '''
- >>> timeit.timeit(with_regex_wrapper(line), number=5000)
- 14.298113452276521
- >>> timeit.timeit(with_replace_wrapper(line), number=5000)
- 5.418057598813073
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement