Re: FAQ 6.9 What is "/o" really for?
- From: Mark Clements <mark.clementsREMOVETHIS@xxxxxxxxxx>
- Date: Fri, 10 Nov 2006 21:51:41 +0100
Mark Clements wrote:
Dr.Ruud wrote:<snip>John Bokma schreef:
print if $regex;
ITYM:
print if /$regex/;
It works OK if the match is written out longhand.
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
.
- Follow-Ups:
- Re: FAQ 6.9 What is "/o" really for?
- From: Dr.Ruud
- Re: FAQ 6.9 What is "/o" really for?
- References:
- FAQ 6.9 What is "/o" really for?
- From: PerlFAQ Server
- Re: FAQ 6.9 What is "/o" really for?
- From: John Bokma
- Re: FAQ 6.9 What is "/o" really for?
- From: Dr.Ruud
- Re: FAQ 6.9 What is "/o" really for?
- From: Mark Clements
- FAQ 6.9 What is "/o" really for?
- Prev by Date: Re: Permission Denied error when moving files - Perl
- Next by Date: Re: Sharing variables between modules
- Previous by thread: Re: FAQ 6.9 What is "/o" really for?
- Next by thread: Re: FAQ 6.9 What is "/o" really for?
- Index(es):
Relevant Pages
|
Loading