> I hope I don't have any bugs here :)

Just one.  :)

Your expressions all say \d instead of \d? for the second digit in
each set, while the simple one correctly has \d?.   So your
expressions had an unfair advantage and as a result finish faster.

Add \d? to your expressions, and you should find that "simple",
besides probably being the easiest to read, is also the most efficient
of the expressions listed.  (Note that I removed the obviously
inefficient ones.)

use strict;
use warnings;
use Benchmark 'cmpthese';

my $field = '19.12.12';

cmpthese -5, {
       simple => sub {
               if ( $field =~ /^[1-9]\d?\.[1-9]\d?\.[1-9]\d?$/ ) {}
       },
       short => sub {
               if ( $field =~ /^([1-9]\d?\.){2}[1-9]\d?$/ ) {}
       },
       shortwoc => sub {
               if ( $field =~ /^(?:[1-9]\d?\.){2}[1-9]\d?$/ ) {}
       },
       trickywoc => sub {
               if ( $field =~ /^(?:[1-9]\d?(?:\.[1-9]\d?){2})$/ ) {}
       },
};

__END__               Rate     short  shortwoc trickywoc    simple
short      677138/s        --      -23%      -25%      -40%
shortwoc   880536/s       30%        --       -2%      -22%
trickywoc  901095/s       33%        2%        --      -20%
simple    1127127/s       66%       28%       25%        --

second benchmark:
my $field = '19.12.02';

Rate     short  shortwoc trickywoc    simple
short      927396/s        --      -10%      -12%      -29%
shortwoc  1027728/s       11%        --       -3%      -22%
trickywoc 1055892/s       14%        3%        --      -19%
simple    1310837/s       41%       28%       24%        --

> Have a nice day.

You too.  I learned a lot from this, so thanks.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to