このサンプル コードは、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);
私はこの出力を得ました
$ perl hashs.pl 文字列ハッシュ開始:1308199085.84375 文字列ハッシュ所要時間:500 ミリ秒 Int ハッシュ開始:1308199086.34379 Int ハッシュ所要時間:428 ミリ秒
Cygwin (Windows) でこれを試しています。Perl のバージョンは 5.10.1 です。
ここでいくつか質問があります。2)文字列を整数に変換する場合、文字列を保存する代わりに、パフォーマンスが向上するかどうか 3) multihash のキーとして 64 ビット値を保持する必要がある場合、bigint のパフォーマンスが向上するか、64 ビット値を文字列として保持します。