moritz has asked for the wisdom of the Perl Monks concerning the following question:
Somehow I thought that the possessive quantifiers in perl 5.10 could only boost up performance, and never degrade it (neglecting a bit of overhead, perhaps). It seems that I was wrong:
You can see that the normal quantifier is the fastest, more than 20 times faster than the possessive quantifier.#!/usr/bin/perl use strict; use warnings; use 5.010; use Benchmark qw(cmpthese); my $str = "bea" x 100; my $re = qr/(?:be|ea|a)/; cmpthese(-2, { atomic1 => sub { die if $str =~ m/(?>$re+)\d/ }, atomic2 => sub { die if $str =~ m/(?>$re)+\d/ }, normal => sub { die if $str =~ m/$re+\d/ }, posessive => sub { die if $str =~ m/$re++\d/ }, }); __END__ Rate posessive atomic1 atomic2 normal posessive 93.3/s -- 0% -96% -97% atomic1 93.3/s 0% -- -96% -97% atomic2 2545/s 2628% 2628% -- -8% normal 2764/s 2862% 2862% 9% --
But why?
I tried to decipher the -Mre=debug output, and it seems that, with a normal quantifier, the regex engine does some backtracking, so it can't be a magic optimization that immediately proves that the match fails.
Or is my benchmark flawed in some way?
Update 1: I talked a bit on IRC with demerphq, and as he wrote the problem seems to be some caching problem.
So far it's not clear if 5.8.8 is caching too eagerly, or 5.10.0 too little.
It's also clear that the pattern isn't very useful, in most cases such a pattern would be anchored, in which case it's blazingly fast ;-)
I also informed our formidable porters.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Performance of possessive quantifiers
by hipowls (Curate) on Jan 27, 2008 at 19:26 UTC | |
by moritz (Cardinal) on Jan 27, 2008 at 22:02 UTC | |
by hipowls (Curate) on Jan 27, 2008 at 22:55 UTC | |
Re: Performance of possessive quantifiers
by tsee (Curate) on Jan 28, 2008 at 11:07 UTC | |
by demerphq (Chancellor) on Jan 28, 2008 at 14:09 UTC | |
by tsee (Curate) on Jan 28, 2008 at 16:14 UTC | |
by moritz (Cardinal) on Jan 28, 2008 at 16:51 UTC |
Back to
Seekers of Perl Wisdom