That is odd, because my benchmark indicates the opposite. Here is
some benchmark code calling the two version with longer strings and
more cuts each time. The results (shown at the end, slightly
reformatted: perl 5.6.1) indicate a fairly predictable increase in
efficiency of the unpack() method versus the substr() method:
#!/usr/bin/perl -w
use strict;
use Benchmark;
my @args = ('ACGTACGTACGT', 2,4,5);
timethese(-2,{
japhy => sub{japhy(@args)},
danger => sub{danger(@args)},
});
@args = ('ACGTACGTACGT' x 4, (2,4,5) x 4);
timethese(-2,{
japhy => sub{japhy(@args)},
danger => sub{danger(@args)},
});
@args = ('ACGTACGTACGT' x 100, (2,4,5) x 100);
timethese(-2,{
japhy => sub{japhy(@args)},
danger => sub{danger(@args)},
});
sub danger {
my $s = shift;
join '-', unpack join('A','',@_,'*') ,$s;
}
sub japhy {
my $s = shift;
join '-', map(substr($s, 0, $_, ''), @_),$s;
}
__END__
Benchmark: running danger, japhy, each for at least 2 CPU seconds...
danger: 1 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU)
@ 10438.35/s (n=21503)
japhy: 1 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU)
@ 8694.17/s (n=17910)
Benchmark: running danger, japhy, each for at least 2 CPU seconds...
danger: 2 wallclock secs ( 2.03 usr + 0.00 sys = 2.03 CPU)
@ 4800.49/s (n=9745)
japhy: 3 wallclock secs ( 2.10 usr + 0.00 sys = 2.10 CPU)
@ 3409.05/s (n=7159)
Benchmark: running danger, japhy, each for at least 2 CPU seconds...
danger: 2 wallclock secs ( 2.13 usr + 0.00 sys = 2.13 CPU)
@ 258.22/s (n=550)
japhy: 3 wallclock secs ( 2.01 usr + 0.00 sys = 2.01 CPU)
@ 155.72/s (n=313)
Of course, it is entirely possible that I've completely messed up
the benchmark.
|