Why is regex search slower with capturing groups in Python? : <p>I have an application code which generates regexes dynamically
from a config for some parsing.
When timing performance of two variations, the regex variation
with each part of an OR regex being captured is noticably slow than
a normal regex.
The reason would be overhead of certain operations internally in regex module.</p>

<pre><code>&gt;&gt;&gt; import timeit
&gt;&gt;&gt; setup = '''
... import re
... '''   

#no capture group 
&gt;&gt;&gt; print(timeit.timeit("re.search(r'hello|bye|ola|cheers','some say hello,some say bye, or ola or cheers!')", setup=setup))
0.922958850861

#with capture group
&gt;&gt;&gt; print(timeit.timeit("re.search(r'(hello)|(bye)|(ola)|(cheers)','some say hello,some say bye, or ola or cheers!')", setup=setup))
1.44321084023

#no capture group
&gt;&gt;&gt; print(timeit.timeit("re.search(r'hello|bye|ola|cheers','some say hello,some say bye, or ola or cheers!')", setup=setup))
0.913202047348

# capture group
&gt;&gt;&gt; print(timeit.timeit("re.search(r'(hello)|(bye)|(ola)|(cheers)','some say hello,some say bye, or ola or cheers!')", setup=setup))
1.41544604301
</code></pre>

<p><strong>Question:</strong> What causes this considerable drop in performance when using capture groups ? </p>
