I added a couple other tests (AddressC and AddressD) to the mix. Basically, I wanted to take Regexp::Common out of the code to see if a standard regexp showed the same behavior. So, I grabbed the regular expression from Regexp::Common and placed it in the code directly. In AddressC, I put it directly in the subtype (similar to Address A):
subtype USZipCodeC => as Value => where { $_ =~ /(?-xism:^(?:(?:(?:USA?)-){0,1}(?:(?:(?:[0-9]{3})(?: +[0-9]{2}))(?:(?:-)(?:(?:[0-9]{2})(?:[0-9]{2}))){0,1}))$)/; }; has 'zip_code' => (is => 'rw', isa => 'USZipCodeC');
For AddressD, I compile the regexp (similar to AddressB):
So, I would expect that AddressB and AddressD would be the fastest. Here the result of a benchmark on my WinXP/Strawberry Perl 5.10 setup:my $zip_re = qr/(?-xism:^(?:(?:(?:USA?)-){0,1}(?:(?:(?:[0-9]{3})(?:[0- +9]{2}))(?:(?:-)(?:(?:[0-9]{2})(?:[0-9]{2}))){0,1}))$)/; subtype USZipCodeD => as Value => where { $_ =~ $zip_re; }; has 'zip_code' => (is => 'rw', isa => 'USZipCodeD');
Rate A D C B A 2960/s -- -42% -42% -42% D 5061/s 71% -- -1% -1% C 5099/s 72% 1% -- -0% B 5108/s 73% 1% 0% --
The method using Regexp::Common directly in the Subtype is the slowest of all the techniques (AddressA). Basically, the other three are tied. So, the compiled regexp doesn't seem to make a difference as long as I avoid Regexp::Common.
In reply to Re^2: Slowdown when using Moose::Util::TypeConstraints with Regexp::Common
by kevbot
in thread Slowdown when using Moose::Util::TypeConstraints with Regexp::Common
by kevbot
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |