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):

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');
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:
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.