No such thing as a small change | |
PerlMonks |
Re^5: Performance penalty of using qr//by dave_the_m (Monsignor) |
on Dec 21, 2017 at 20:01 UTC ( [id://1205999]=note: print w/replies, xml ) | Need Help?? |
You've created a benchmark where the cost of compiling a single regex containing 45000 alternations is overwhelmingly more expensive than running that compiled regex 11 times.
In the qr case, the pattern is compiled once, *before* the benchmark is run. The benchmark cost is 11 matches, plus 11 clonings of the compiled regex's internal struture. In the str case, the benchmark includes compiling the pattern before matching the first word. For the subsequent 10 matches it attempt to recompile /$str/, but each time it uses an optimisation where it sees if the string has changed since last time and if so skips recompiling it. So sometimes /$str/ in a loop for unchanging $str can be faster than /$qr/, but that benchmark won't show it. Or more formally, if C is the time to compile a pattern, M is the time to run (match) against the compiled pattern, and D is the time to duplicate the regex structure, then takes C + N(M+D), but your benchmark was measuring N(M+D); while takes Dave.
In Section
Seekers of Perl Wisdom
|
|