Welcome to The Monastery, and thank you for writing your node.
I have little experience with the Benchmark module, but after conducting some tests, I would say that you should use the latter method, the one you prefer. It is more concise, easier to read, and a little more efficient. (thanks for the notice, ikegami.)
#!/usr/bin/perl
use warnings;
use strict;
use 5.010;
use Benchmark qw(:all);
use Data::Dumper;
cmpthese(100000, {
'LowOutside' => sub {
my @array = qw(8 1 3 5 6 2);
@array = sort @array;
foreach (@array) {
my $num = $_;
}
},
'LowInside' => sub {
my @array = qw(8 1 3 5 6 2);
foreach (sort @array) {
my $num = $_;
}
},
'Outside' => sub {
my @array = qw(8 1 3 5 6 2) x 100;
@array = sort @array;
foreach (@array) {
my $num = $_;
}
},
'Inside' => sub {
my @array = qw(8 1 3 5 6 2) x 100;
foreach (sort @array) {
my $num = $_;
}
}
});
__END__
Rate Outside Inside LowOutside LowInside
Outside 1681/s -- -1% -99% -99%
Inside 1691/s 1% -- -99% -99%
LowOutside 169492/s 9985% 9924% -- -5%
LowInside 178571/s 10525% 10461% 5% --
And you didn't even know bears could type.