Named regexp groups are nearly as fast
I
love the
(?P<foo>) named regexp groups in Python. They make the
code so much more readable! But are they slower? Not much.
timeit.py -r 50
-s 'import re; r = re.compile("foo (?P<x>bar)")'
'm = r.match("foo bar"); g = m.group("x")'
100000 loops, best of 50: 3.34 usec per loop
timeit.py -r 50
-s 'import re; r = re.compile("foo (bar)")'
'm = r.match("foo bar"); g = m.group(1)'
100000 loops, best of 50: 3.14 usec per loop
The named groups version is about 6% slower. Consistent, but not very
significant.
-s 'import re; r = re.compile("foo (?P<x>bar)")'
'm = r.match("foo bar"); g = m.group("x")'
100000 loops, best of 50: 3.34 usec per loop
timeit.py -r 50
-s 'import re; r = re.compile("foo (bar)")'
'm = r.match("foo bar"); g = m.group(1)'
100000 loops, best of 50: 3.14 usec per loop