Using perl's 'times' to get the cpu times before and after the following loops shows me that, for some reason, the precompiled regex version is actually about 33% slower than the inline regex. I did the regex match twice to be close to the example code and to prevent any mysterious perl optimization across loop runs.
for (1..$num_runs) {
$test_string =~ $pcr;
$test_string =~ $pcr;
}
and
for(1..$num_runs) {
$test_string =~ m/\s*[\/\\]?\s*/;
$test_string =~ m/\s*[\/\\]?\s*/;
}
With $num_runs
being 10,000,000 and $pcr
and $test_string
being the following:
my $pcr = qr/\s*[\/\\]?\s*/;
my $test_string = '<what>';
The cpu times after finding the delta and averaging were:
------------------------------
Precompiled regex:
------------------------------
user : 0.0000040190
system : 0.0000000010
------------------------------
Inline regex:
------------------------------
user : 0.0000030580
system : 0.0000000000
I did not use perl's Benchmark.pm for personal reasons. I've seen it give obviously wrong numbers and, while they were minimal, benchmarking is pointless if you've got numbers you can't trust. These numbers I can trust, though the tests I benchmarked might need to be reevaluated.