I was just curious about it, so here you go: #!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw( cmpthese );
my @data = split /\n/, <<END;
800-800-1212
(800)800-1212
(800) 800-1212
800 800 1212
END
sub dosloop {
my $re = shift;
(my $phone = $_) =~ s/$re//g foreach @data;
}
sub doyloop {
my $fake = shift;
(my $phone = $_) =~ y/0-9//cd foreach @data;
}
cmpthese(-10, {
single => sub { dosloop(qr/\D/) },
multiple=> sub { dosloop(qr/\D+/) },
y => sub { doyloop(qr/\D/) }, # Just a fake parameter
}
);
I tried to be as fair as possible and have the same overhead for all calls. The results are the following: Rate multiple single y
multiple 95932/s -- -6% -39%
single 101881/s 6% -- -35%
y 156303/s 63% 53% --
The simple translation outperforms the other, as expected. I was particularly curious about putting the '+' or not. I vaguely remember some discussion about it before, but I couldn't tell you where.
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Don't fool yourself.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|