I have nothing to add to liverpole's suggestion, but the way you're doing your benchmark test. There's a Benchmark module available, which is really easy to use. If you write a benchmark code with it once, you'll never write a time calculation again. It is useful to compare results too.
Here is a code that compares liverpole's suggestion and the way you tried it first:
use strict;
use warnings;
use Benchmark qw(:all);
my $limit = 1_000;
sub do_it_tcarmeli {
my %do_it_tcarmeli;
foreach my $i ( 0 .. $limit ) {
$do_it_tcarmeli{$i} = 1;
}
return;
}
my %do_it_liverpole;
sub do_it_liverpole {
foreach my $i ( 0 .. $limit ) {
$do_it_liverpole{$i} = 1;
}
return;
}
cmpthese(
-10,
{
do_it_tcarmeli => \&do_it_tcarmeli,
do_it_liverpole => \&do_it_liverpole,
}
);
Igor 'izut' Sutton
your code, your rules.