A. Sinan Unur said:
That looks contrived to me. If I were to use substr here, I would use:
'substr_3arg' => sub {
my @lines = gen_lines();
$_ = substr($_, 7) for @lines;
},
Nope, not contrived... I just honestly didn't think about doing it that
way. For some reason, I seem to have a bizarre fondness for using
substr() as an l-value. It didn't occur to me to go backwards from the
way I was originally thinking.
Thanks for pointing that out. For the sake of completeness, I offer a
benchmark with all four methods....
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw/cmpthese/;
sub gen_lines {
qw{
Http://www.yahoo.com
Http://gmail.google.com
Http://disney.go.com
Http://www.perldoc.com
Http://search.cpan.org
Http://www.oreilly.com
Http://www.ge.com
Http://www.stratus.com
Http://www.es11.com
Http://www.bankofamerica.com
};
}
cmpthese(-10, {
'substr_2arg' => sub {
my @lines = gen_lines();
$_ = substr($_, 7) for @lines;
},
'substr_3arg' => sub {
my @lines = gen_lines();
substr($_, 0, 7) = q{} for @lines;
},
'substr_4arg' => sub {
my @lines = gen_lines();
substr($_, 0, 7, q{}) for @lines;
},
's///' => sub {
my @lines = gen_lines();
s{^Http://}{} for @lines;
}
}
);
__END__
Benchmark: running s///, substr_2arg, substr_3arg, substr_4arg, each
for at least 10 CPU seconds...
s///: 12 wallclock secs (10.55 usr + 0.01 sys = 10.56 CPU) @
10819.89/s (n=114258)
substr_2arg: 11 wallclock secs (10.02 usr + 0.00 sys = 10.02 CPU) @
13797.90/s (n=138255)
substr_3arg: 12 wallclock secs (10.78 usr + 0.00 sys = 10.78 CPU) @
10266.42/s (n=110672)
substr_4arg: 12 wallclock secs (10.70 usr + 0.00 sys = 10.70 CPU) @
12289.63/s (n=131499)
Rate substr_3arg s/// substr_4arg substr_2arg
substr_3arg 10266/s -- -5% -16% -26%
s/// 10820/s 5% -- -12% -22%
substr_4arg 12290/s 20% 14% -- -11%
substr_2arg 13798/s 34% 28% 12% --
Paul Lalli