开发者

Is integer index in multi hash is better than string index

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 millise开发者_如何学运维c\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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜