Regex – Capturing Group vs Non-Capturing Group

regex

I tried to test the performance of capturing and non-capturing group of the regex.
By the way, there is very slightly different between the capturing group and the non-capturing group.
Is this result normal?

[root@Sensor ~]# ll -h sample.log
-rw-r--r-- 1 root root 21M Oct 20 23:01 sample.log

[root@Sensor ~]# time grep -ciP '(get|post).*' sample.log
20000

real    0m0.083s
user    0m0.070s
sys     0m0.010s

[root@Sensor ~]# time grep -ciP '(?:get|post).*' sample.log
20000

real    0m0.083s
user    0m0.077s
sys     0m0.004s

Best Answer

Typically, non-capturing groups perform better than capturing groups, because they require less allocation of memory, and do not make a copy of the group match. However, there are three important caveats:

  • The difference is typically very small for simple, short expressions with short matches.
  • The act of starting a program like grep itself takes a significant amount of time and memory, and may overwhelm any small improvement gained by using non-capturing group(s).
  • Some languages implement capturing and non-capturing groups in the same way, causing the latter to give no performance improvement.
Related Question