in reply to Storing Regex Patterns in Scalars
every time. If you don't give perl a hint that $variable never changes, it will recompile the regex every time. To avoid that, use/$variable/
to tell "this is a variable, but it never changes, so you need to compile it only once". Here's a benchmark program to show the difference:/$variable/o
And here are the results:use Benchmark; my $qr_regex = qr/ab{1,}c{1,}/; my $str_regex = "ab{1,}c{1,}"; my $string = "asdabdabc"; timethese(10000000, { 'string' => sub { $string =~ /$str_regex/ }, 'string/o' => sub { $string =~ /$str_regex/o }, 'qr' => sub { $string =~ /$qr_regex/ }, });
-- saintmikeBenchmark: timing 10000000 iterations of qr, string, string/o... qr: 11 wallclock secs (10.60 usr + 0.00 sys = 10.60 CPU) @ 943396.23/s (n=10000000) string: 11 wallclock secs (11.80 usr + 0.00 sys = 11.80 CPU) @ 847457.63/s (n=10000000) string/o: 7 wallclock secs ( 9.40 usr + 0.02 sys = 9.42 CP U) @ 1061571.13/s (n=10000000)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Storing Regex Patterns in Scalars
by saskaqueer (Friar) on Mar 30, 2004 at 19:44 UTC |