I tried to be as fair as possible and have the same overhead for all calls. The results are the following:#!/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 } );
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.Rate multiple single y multiple 95932/s -- -6% -39% single 101881/s 6% -- -35% y 156303/s 63% 53% --
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
In reply to Benchmark time: Numbers only in a string
by polettix
in thread Numbers only in a string
by powerhouse
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |