Initially, I was looking for a fast way to access a hash ref element (with a default value if no value is available).
So I tried the following :
use strict;
use warnings;
use DateTime;
my $hashref = { };
for (0..249) {
my $lIdx = $_ * 2;
$hashref->{"MYKEY$lIdx"} = "MYVAL$lIdx";
}
sub WithVariable{
my $result = $hashref->{"MYKEY$_[0]"};
return defined $result ? $result : "NONE";
}
sub WithoutVariable{
return defined $hashref->{"MYKEY$_[0]"} ? $hashref->{"MYKEY$_[0]"} : "NONE";
}
$|++;
my @preciousvalues1 = ();
my @preciousvalues2 = ();
my $dt = DateTime->now;
for (1..25000) { for (0..498) { push @preciousvalues1, WithVariable($_) } }
my $lag = DateTime->now - $dt;
print "With a variable: ", $lag->seconds, "\n";
$dt = DateTime->now;
for (1..25000) { for (0..498) { push @preciousvalues2, WithoutVariable($_) } }
$lag = DateTime->now - $dt;
print "Without a variable: ", $lag->seconds, "\n";
print "Done\n";
But the results seem to be quite random, and perl seems to do a lots of stuff after printing the "Done", and takes forever to exit.
My questions are :
- What would be a neat implementation of a function allowing to access values stored in a hash with a default value if no value is found ?
- What the hell is perl doing after it printed "Done" ? Garbage collection ?
Perl version : This is perl, v5.10.1 built for MSWin32-x86-multi-thread