on 07/24/2001 11:45 AM, [EMAIL PROTECTED] at [EMAIL PROTECTED] wrote:
>
> Recently I came across the idiom of using scalar() to get a head
> count of the entries in an array, up until then I'd always used
> '$#array+1', interested to see if there was a good reason why I
> should use this new idiom, I did a bit of benchmarking with the
> included script and got the following results
>
> Out put from first run:
> Benchmark: timing 125000 iterations of addition_to_$#test, scalar_derived...
> addition_to_$#test: 1 secs ( 2.15 usr 0.00 sys = 2.15 cpu)
> scalar_derived: 2 secs ( 1.18 usr 0.00 sys = 1.18 cpu)
>
> Benchmark: timing 350000 iterations of addition_to_$#test, scalar_derived...
> addition_to_$#test: 5 secs ( 6.00 usr 0.00 sys = 6.00 cpu)
> scalar_derived: 2 secs ( 2.78 usr 0.00 sys = 2.78 cpu)
>
> Being impressed by the fact the scalar derived method didn't change
> at all even though the iterations were doubled, I got to wondering
> about the sudden leap in the time required by the addition_to_$#test
> method and if I was ever going to require a script to carry out this
> operation 350,000 times to merit the extra overhead incurred by using
> scalar() and ran the script again:
>
> Out put from second run:
> Benchmark: timing 125000 iterations of addition_to_$#test, scalar_derived...
> addition_to_$#test: 1 secs ( 1.73 usr 0.00 sys = 1.73 cpu)
> scalar_derived: 2 secs ( 1.28 usr 0.00 sys = 1.28 cpu)
>
> Benchmark: timing 350000 iterations of addition_to_$#test, scalar_derived...
> addition_to_$#test: 5 secs ( 4.80 usr 0.00 sys = 4.80 cpu)
> scalar_derived: 4 secs ( 3.73 usr 0.00 sys = 3.73 cpu)
>
> Anyone got a clue as to why the second run sees the values for the
> scalar_derived method has almost doubled? Is this a memory leak in
> MacPerl or something else?
>
>
>
> #!perl-w
> use Benchmark;
>
>
> @test = (1, 2, 3,4,5,6,7,);
>
> # Use Perl code in strings...
> timethese(125000, {
> 'scalar_derived' => sub {$number = scalar (@test);},
> 'addition_to_$#test' => sub {$number =$#test+1;},
> });
>
>
> timethese(350000, {
> 'scalar_derived' => sub {$number = scalar (@test);},
> 'addition_to_$#test' => sub {$number =$#test+1;},
> });
> __END__
interestingly under 5.6.1a4 I get something like this:
#!perl-w
use Benchmark;
use strict;
use POSIX qw/strftime/;
sub now { strftime( "%H:%M:%S", localtime(time)) };
print now(), "\n";
my @test = (1, 2, 3, 4, 5, 6, 7);
# Use Perl code in strings...
timethese(-5, {
'scalar_derived' => sub { my $number = scalar(@test)},
'addition_to_$#test' => sub { my $number = $#test+1},
});
print now(), "\n";
__END__
14:46:12
Benchmark: running addition_to_$#test, scalar_derived, each for at least 5
CPU seconds...
addition_to_$#test: 6 wallclock secs ( 5.65 usr + 0.00 sys = 5.65 CPU) @
498240.53/s (n=2815059)
scalar_derived: 6 wallclock secs ( 5.27 usr + 0.00 sys = 5.27 CPU) @
748308.04/s (n=3941089)
14:47:28
it took a minute and sixteen seconds to run a test of 2 x 5 seconds? god
forbid I set it to 30 seconds.. Is there something wrong with Benchmark
under 5.6.1a4 ?
still here it looks as thought scalar(@array) is faster by some 250,000
iterations per second, roughly
--
Scott R. Godin | e-mail : [EMAIL PROTECTED]
Laughing Dragon Services | web : http://www.webdragon.net/