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.
>>> import timeit
>>> setup = '''
... import re
... '''
#no capture group
>>> 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
>>> 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
>>> 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
>>> print(timeit.timeit("re.search(r'(hello)|(bye)|(ola)|(cheers)','some say hello,some say bye, or ola or cheers!')", setup=setup))
1.41544604301
Question: What causes this considerable drop in performance when using capture groups ?