Skip to content

Instantly share code, notes, and snippets.

@a-recknagel
Last active August 29, 2019 12:20
Show Gist options
  • Save a-recknagel/739214fef3b08c9344bacf9682e1d01f to your computer and use it in GitHub Desktop.
Save a-recknagel/739214fef3b08c9344bacf9682e1d01f to your computer and use it in GitHub Desktop.
Timing re operations
# code that I would write in an interactive session to perform a re.search or re.sub
# setup code
>>> import re
>>> text = open('lorem_ipsum.txt').read() # filled with https://loremipsum.io/generator/?n=99&t=p
>>> regex = re.compile('foo')
# test code for re.search
>>> regex.search(text) # returns None, there is no foo in lorem ipsum it seems
# test code for re.sub
>>> regex.sub('bar', text) # returns lorem ipsum unchanged
Lorem ipsum dolor sit amet, ...
[...]
... sagittis vitae et leo duis ut diam.
# calling timeit from the command line, to ensure clean interpreter state
# timing call for re.search
$ python -m timeit -s "import re; text=open('lorem_ipsum.txt').read(); reg = re.compile('foo')" "reg.search(text)"
10000 loops, best of 5: 26.3 usec per loop
# timing call for re.sub
$ python -m timeit -s "import re; text=open('lorem_ipsum.txt').read(); reg = re.compile('foo')" "reg.sub('bar', text)"
10000 loops, best of 5: 25.4 usec per loop
# looks to be about the same speed, definiteliy not worth it to re.search in order to know if re.sub does something.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment