多重哈希中的整数索引是否优于字符串索引
我编写此示例代码是为了检查 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 hash take:428 millisec
我正在 Cygwin (Windows) 中尝试这个,Perl 版本是 5.10.1
我这里有几个问题 1)当我们在 Hash 中存储一个整数时,是否会计算该整数的哈希键,或者 Perl 直接使用存储桶中的值? 2)如果我将字符串转换为整数,而不是存储字符串,我是否会获得任何性能改进? 3)如果我需要保留64位值作为multihash中的键,这将提供更好的性能bigint或将64位值保留为字符串
I wrote this sample code to check whether integer or string index is better in perl hash.
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);
I got this output
$ perl hashs.pl
String hash start:1308199085.84375
String hash took:500 millisec
Int hash start:1308199086.34379
Int hash took:428 millisec
I am trying this in Cygwin (Windows) and Perl version is 5.10.1
I have couple of questions here
1)When we store a integer in Hash whether a hash key is computed for that or Perl uses the vale directly in the bucket?
2)Instead of storing a string if i convert the same to a integer whether i will get any performance improvements?
3)If i need to keep a 64 bit value as the key in multihash , which will give better performance bigint or keep the 64bit value as a string
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Perl 中的哈希仅将字符串作为键。因此,您的
$int_hash
的键无论如何都被强制转换为字符串,因此两个版本之间的运行时间差异应该可以忽略不计。Hashes in Perl only ever have strings as keys. So your
$int_hash
's keys are all coerced into strings anyway, and so any difference in run time between the two versions should be negligible.