Re: FAQ 6.9 What is "/o" really for?



Mark Clements wrote:
Dr.Ruud wrote:
John Bokma schreef:

print if $regex;

ITYM:

print if /$regex/;


It works OK if the match is written out longhand.
<snip>

Just as a matter of interest, I ran some timing tests:

mark@owl:~$ cat testretimings.pl
use strict;
use warnings;

use Benchmark;


my $regex = qr(s\s[\w{1,2}]\s);
my $regexo = qr(s\s[\w{1,2}]\s)o;
my $text = "this is a fairly short string";
my $count = 1000000;

timethese (
$count,
{
form1 => q( $text =~ /$regex/ ),
form2 => q( $text =~ /$regex/o ),
form3 => q( $text =~ $regex ),
form4 => q( $text =~ /$regexo/ ),
form5 => q( $text =~ /$regexo/o ),
form6 => q( $text =~ $regexo ),
}

);
mark@owl:~$ perl testretimings.pl
Benchmark: timing 1000000 iterations of form1, form2, form3, form4, form5, form6...
form1: 1 wallclock secs ( 0.82 usr + 0.00 sys = 0.82 CPU) @ 1219512.20/s (n=1000000)
form2: 0 wallclock secs ( 0.71 usr + 0.00 sys = 0.71 CPU) @ 1408450.70/s (n=1000000)
form3: 1 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 1250000.00/s (n=1000000)
form4: 1 wallclock secs ( 0.81 usr + 0.00 sys = 0.81 CPU) @ 1234567.90/s (n=1000000)
form5: 0 wallclock secs ( 0.72 usr + 0.00 sys = 0.72 CPU) @ 1388888.89/s (n=1000000)
form6: 1 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 1250000.00/s (n=1000000)


Within the limited testing done here, form2 and form 5 are consistently marginally faster than the other two. However, the doc for qr (man perlop) says:

<quote>
Precompilation of the pattern into an internal representation at the moment of qr() avoids a need to recompile the pattern every time a match "/$pat/" is attempted. (Perl has many other internal optimizations, but none would be triggered in the above example if we did not use
qr() operator.)
</quote>

I don't understand why /o makes a different (albeit a minor one) in cases form2 and form5 if the pattern has already been compiled. Am I missing something obvious?



Mark
.



Relevant Pages

  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)
  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)
  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)
  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)
  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)

Loading