2011-06-16 67 views
1

我寫了這個示例代碼來檢查整數或字符串索引在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毫秒 詮釋哈希開始:1308199086.34379 詮釋哈希了:428毫秒

我試圖在Cygwin(Windows)和Perl版本是5.10.1

我有幾個問題在這裏 1)Whe n我們在Hash中存儲一個整數是否爲其計算散列鍵,或者Perl是否直接在存儲區中使用該值? 2)而不是存儲一個字符串,如果我將其轉換爲整數,無論​​我會得到任何性能改進? 3)如果我需要保留一個64位的值作爲multihash中的鍵值,這將提供更好的性能bigint或將64bit值保留爲一個字符串

+5

使用基準測試模塊 – ysth 2011-06-16 04:51:08

+0

如果您使用從0開始的連續整數,則應該使用一個數組,該數組的速度將顯着快於散列。 – 2011-06-16 20:48:43

+0

它不順序並從一個bigint文件中讀取數據 – Raghuram 2011-06-17 05:23:54

回答

13

Perl中的Hashes只有字符串作爲鍵。因此,無論如何你的$int_hash的鍵都被強制爲字符串,所以兩個版本之間運行時間的差異應該可以忽略不計。