Rag*_*ram 1 perl perl-data-structures
我编写了这个示例代码来检查perl哈希中的整数或字符串索引是否更好.
use Time::Local;
use Time::HiRes qw/gettimeofday/;
my %string_hash;
my %int_hash;
$i_count = 100;
$j_count = 100;
$k_count = 1000;
foreach $i (0..$i_count)
{
foreach $i (0..$j_count)
{
foreach $k (0..$k_count)
{
$i += 0;$j += 0;$k += 0;
$int_hash{$i}->{$j}->{$k} = 1;
$string_hash{"$i"}->{"$j"}->{"$k"} = 1;
}
}
}
my $profile = gettimeofday();
print "String hash start:$profile\n";
foreach $i (keys %string_hash)
{
foreach $j(keys %{ $string_hash{$i} })
{
foreach $k(keys %{ $string_hash{$i}{$j} })
{
$i += 0;$j += 0;$k += 0;
$val = $string_hash{$i}->{$j}->{$k};
}
}
}
printf("String hash took:%d millisec\n", (gettimeofday()-$profile)*1000);
$profile = gettimeofday();
print "Int hash start:$profile\n";
foreach $i (keys %int_hash)
{
foreach $j(keys %{ $int_hash{$i} })
{
foreach $k(keys %{ $int_hash{$i}{$j} })
{
$i += 0;$j += 0;$k += 0;
$val = $int_hash{$i}->{$j}->{$k};
}
}
}
printf("Int hash took:%d millisec\n", (gettimeofday()-$profile)*1000);
Run Code Online (Sandbox Code Playgroud)
我得到了这个输出
$ perl hashs.pl String hash start:1308199085.84375 String hash:500 millisec Int hash start:1308199086.34379 Int hash take:428 millisec
我在Cygwin(Windows)中尝试这个,Perl版本是5.10.1
我在这里有几个问题1)当我们在Hash中存储一个整数时,是否为此计算了哈希键,或者Perl是否直接在桶中使用了该值?2)如果我将相同的值转换为整数,我是否会获得任何性能改进,而不是存储字符串?3)如果我需要将64位值保留为multihash中的键,这将提供更好的性能bigint或将64位值保持为字符串