When I saw your post I thought "Yes, that's true. But it would be more efficient to declare the @good_nums array outside the look and just empty it". Luckily I did not go ahead and post that, but run a benchmark (In Perl 5.8, ActivePerl build 804, Win2kServer):
use Benchmark;
use strict;
sub arrayInit {
foreach (1..1000) {
my @list;
foreach (1..10) {
push @list, $_;
}
}
}
sub arrayUndef {
my @list;
foreach (1..1000) {
foreach (1..10) {
push @list, $_;
}
undef @list;
}
}
sub arrayEmpty {
my @list;
foreach (1..1000) {
foreach (1..10) {
push @list, $_;
}
@list = ();
}
}
sub scalarInit {
foreach (1..1000) {
my $sum;
foreach (1..10) {
$sum += $_;
}
}
}
sub scalarUndef {
my $sum;
foreach (1..1000) {
foreach (1..10) {
$sum += $_;
}
undef $sum;
}
}
sub scalarEmpty {
my $sum;
foreach (1..1000) {
foreach (1..10) {
$sum += $_;
}
$sum = 0;
}
}
timethese 1000, {
arrayInit => \&arrayInit,
arrayUndef => \&arrayUndef,
arrayEmpty => \&arrayEmpty,
scalarInit => \&scalarInit,
scalarUndef => \&scalarUndef,
scalarEmpty => \&scalarEmpty,
}
__END__
Benchmark: timing 1000 iterations of arrayEmpty, arrayInit, arrayUndef
+, scalarEmpty, scalarInit, sca
larUndef...
arrayEmpty: 15 wallclock secs (13.97 usr + 0.00 sys = 13.97 CPU) @ 71
+.58/s (n=1000)
arrayInit: 13 wallclock secs (13.45 usr + 0.00 sys = 13.45 CPU) @ 74
+.35/s (n=1000)
arrayUndef: 16 wallclock secs (15.70 usr + 0.00 sys = 15.70 CPU) @ 63
+.68/s (n=1000)
scalarEmpty: 9 wallclock secs ( 9.30 usr + 0.02 sys = 9.32 CPU) @ 1
+07.26/s (n=1000)
scalarInit: 10 wallclock secs ( 9.36 usr + 0.01 sys = 9.37 CPU) @ 10
+6.68/s (n=1000)
scalarUndef: 10 wallclock secs ( 9.43 usr + 0.00 sys = 9.43 CPU) @ 1
+06.01/s (n=1000)
So once again I was wrong :-|
Thanks for making me try.
Jenda
Always code as if the guy who ends up maintaining your code
will be a violent psychopath who knows where you live.
-- Rick Osborne
Edit by castaway: Closed small tag in signature |