Uri Guttman said:
BM> However, it's not so fast...
you chose a bad data set for this.
!?
my @data = ( 'a' x 1000 ) x 1000 ;
Benchmark: running map, substr1, substr2, substr3 for at least 2 CPU seconds...
map: 2 wallclock secs ( 2.22 usr + 0.00 sys = 2.22 CPU) @ 127.48/s (n=283)
substr1: 2 wallclock secs ( 2.18 usr + 0.00 sys = 2.18 CPU) @ 293.12/s (n=639)
substr2: 2 wallclock secs ( 2.07 usr + 0.00 sys = 2.07 CPU) @ 347.34/s (n=719)
substr3: 2 wallclock secs ( 2.05 usr + 0.00 sys = 2.05 CPU) @ 317.07/s (n=650)
now 4 arg substr is much faster.
this is why benchmarking is an art unto itself. it is so easy to get
results that support wrong conclusions.
Indeed - in this case you've chosen the conclusion you want and
written a benchmark to support it. This ensures the conclusion that
will be supported is the "right" one.
Removing the first character from an array of string is typically
something one does to plaintext. And plaintext is typically ~70-120
characters wide.
Without knowing the OP's real problem domain it's just as likely that
it is you who've chosen a bad data set that supports the wrong
conclusion.
But then again I'm not even able to reproduce your results...
use Benchmark;
my %tests;
for ( my $i = 1; $i <= 1024 ; $i*=2 ) {
my @data = ('X' x $i) x 1000;
$tests{sprintf('4arg%4d',$i)} = sub {
my @array = @data;
substr $_, 0, 1, '' for @array;
};
$tests{sprintf('copy%4d',$i)} = sub {
my @array = @data;
$_ = substr $_, 1 for @array;
};
}
timethese -2 => \%tests;
__END__
Benchmark: running 4arg 1, 4arg 2, 4arg 4, 4arg 8, 4arg 16, 4arg 32, 4arg 64, 4arg 128, 4arg 256, 4arg 512, 4arg1024, copy 1, copy 2, copy 4, copy 8, copy 16, copy 32, copy 64, copy 128, copy 256, copy 512, copy1024, each for at least 2 CPU seconds...
4arg 1: 2 wallclock secs ( 2.19 usr + 0.00 sys = 2.19 CPU) @ 642.01/s (n=1406)
4arg 2: 2 wallclock secs ( 2.02 usr + 0.00 sys = 2.02 CPU) @ 642.57/s (n=1298)
4arg 4: 2 wallclock secs ( 2.19 usr + 0.00 sys = 2.19 CPU) @ 642.01/s (n=1406)
4arg 8: 2 wallclock secs ( 2.16 usr + 0.00 sys = 2.16 CPU) @ 592.13/s (n=1279)
4arg 16: 2 wallclock secs ( 2.16 usr + 0.00 sys = 2.16 CPU) @ 592.13/s (n=1279)
4arg 32: 3 wallclock secs ( 2.13 usr + 0.00 sys = 2.13 CPU) @ 572.77/s (n=1220)
4arg 64: 2 wallclock secs ( 2.16 usr + 0.00 sys = 2.16 CPU) @ 518.06/s (n=1119)
4arg 128: 2 wallclock secs ( 2.03 usr + 0.00 sys = 2.03 CPU) @ 354.19/s (n=719)
4arg 256: 2 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 229.13/s (n=472)
4arg 512: 2 wallclock secs ( 2.02 usr + 0.09 sys = 2.11 CPU) @ 135.55/s (n=286)
4arg1024: 2 wallclock secs ( 1.74 usr + 0.27 sys = 2.01 CPU) @ 77.61/s (n=156)
copy 1: 2 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 702.91/s (n=1448)
copy 2: 2 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 702.91/s (n=1448)
copy 4: 2 wallclock secs ( 2.07 usr + 0.00 sys = 2.07 CPU) @ 699.52/s (n=1448)
copy 8: 3 wallclock secs ( 2.01 usr + 0.00 sys = 2.01 CPU) @ 645.77/s (n=1298)
copy 16: 2 wallclock secs ( 2.06 usr + 0.03 sys = 2.09 CPU) @ 606.70/s (n=1268)
copy 32: 2 wallclock secs ( 2.15 usr + 0.00 sys = 2.15 CPU) @ 594.88/s (n=1279)
copy 64: 2 wallclock secs ( 2.08 usr + 0.00 sys = 2.08 CPU) @ 535.10/s (n=1113)
copy 128: 2 wallclock secs ( 2.12 usr + 0.00 sys = 2.12 CPU) @ 339.15/s (n=719)
copy 256: 3 wallclock secs ( 2.09 usr + 0.00 sys = 2.09 CPU) @ 207.66/s (n=434)
copy 512: 2 wallclock secs ( 2.11 usr + 0.00 sys = 2.11 CPU) @ 135.55/s (n=286)
copy1024: 2 wallclock secs ( 2.11 usr + 0.00 sys = 2.11 CPU) @ 79.15/s (n=167)
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\