in reply to Never-to-use Perl features?
Including the /o-modifier, the list now has 3 language features that seemed a nice idea at first, but aren't really usable now.
I've read the thread you quoted and I'm obviously missing something because in my experience /o works exactly as it should:
use Benchmark; my @words = map { chomp; $_ } (<DATA>); my $alpha = '[a-zA-Z]'; my $alnum = '[a-zA-Z0-9]'; timethese(2000, { 'Without /o' => \&testsub, 'With /o' => \&testsubo, }); sub testsub { my $count = 0; foreach (@words) { $count++ if(/^$alpha$alnum+$/); } return $count; } sub testsubo { my $count = 0; foreach (@words) { $count++ if(/^$alpha$alnum+$/o); } return $count; } __DATA__ 1500 words one per line
Which on my system shows that with /o is three times faster than without.
Using variables to give meaningful names to chunks of a regex is very useful for improving the readability, maintainability and reusability of the code. Without /o it would be inefficient. What is it about /o that makes it "not really usable"?
Update: I added this to the test script:
my $qr = qr/^$alpha$alnum+$/; [snip] sub testsubqr { my $count = 0; foreach (@words) { $count++ if(/$qr/); } return $count; } sub testsubqro { my $count = 0; foreach (@words) { $count++ if(/$qr/o); } return $count; }
The qr// approach seems to be about 20% slower than /o and qr// + /o seems to be about the same as /o alone.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Never
by BrowserUk (Patriarch) on May 13, 2003 at 00:56 UTC | |
by edoc (Chaplain) on May 13, 2003 at 04:00 UTC | |
Re: Re: Never
by perrin (Chancellor) on May 13, 2003 at 07:53 UTC | |
by grantm (Parson) on May 13, 2003 at 08:45 UTC | |
Re^2: Never (qr//)
by tye (Sage) on May 13, 2003 at 14:25 UTC | |
by diotalevi (Canon) on May 13, 2003 at 14:44 UTC | |
by tye (Sage) on May 13, 2003 at 15:52 UTC | |
by grantm (Parson) on May 14, 2003 at 09:46 UTC | |
by BrowserUk (Patriarch) on May 14, 2003 at 10:32 UTC |