有效使用Perl哈希

时间:2016-02-17 16:42:17

标签: performance perl hash

我正在使用哈希来缩写州名

2

我的一些输入集已经有缩写的州名。使用%STATEABBRIVATE = ('ALABAMA' => 'AL', ...); 或将另外51个匹配对if defined $STATEABBRIVATE{$state}添加到哈希是否更有效?

2 个答案:

答案 0 :(得分:1)

如果您想验证状态是否真的存在,使用AL => 'AL'可能是最简单的方法。

要保持代码干(不要重复自己),你可以

my %STATEABBRIVATE = ( ALABAMA => 'AL',
                       ...
                     );
my @abbrevs = values %STATEABBRIVATE;
@STATEABBRIVATE{@abbrevs} = @abbrevs;

如果你对性能感到满意,那么瓶颈可能就在其他地方:

#! /usr/bin/perl
use warnings;
use strict;

use Benchmark qw{ cmpthese };
use Test::More;

my %hash = qw( Alabama AL Alaska AK Arizona AZ Arkansas AR California CA
               Colorado CO Connecticut CT Delaware DE Florida FL
               Georgia GA Hawaii HI Idaho ID Illinois IL Indiana IN
               Iowa IA Kansas KS Kentucky KY Louisiana LA Maine ME
               Maryland MD Massachusetts MA Michigan MI Minnesota MN
               Mississippi MS Missouri MO Montana MT Nebraska NE
               Nevada NV Ohio OH Oklahoma OK Oregon OR Pennsylvania PA
               Tennessee TN Texas TX Utah UT Vermont VT Virginia VA
               Washington WA Wisconsin WI Wyoming WY );

$hash{'West Virginia'}  = 'WV';
$hash{'South Dakota'}   = 'SD';
$hash{'South Carolina'} = 'SC';
$hash{'Rhode Island'}   = 'RI';
$hash{'North Dakota'}   = 'ND';
$hash{'North Carolina'} = 'NC';
$hash{'New York'}       = 'NY';
$hash{'New Mexico'}     = 'NM';
$hash{'New Jersey'}     = 'NJ';
$hash{'New Hampshire'}  = 'NH';

my %larger = %hash;
@larger{ values %hash } = values %hash;

sub def {
    my $state = shift;
    return defined $hash{$state} ? $hash{$state} : $state
}

sub ex {
    my $state = shift;
    return exists $hash{$state} ? $hash{$state} : $state
}

sub hash {
    my $state = shift;
    return $larger{$state}
}

is(def($_), ex($_),   "def-ex-$_")   for keys %larger;
is(def($_), hash($_), "def-hash-$_") for keys %larger;

done_testing();

cmpthese(-1,
         { hash => sub { map hash($_), keys %larger },
           ex   => sub { map ex($_),   keys %larger },
           def  => sub { map def($_),  keys %larger },
         });

结果:

        Rate  def   ex hash
def  27307/s   --  -2% -11%
ex   27926/s   2%   --  -9%
hash 30632/s  12%  10%   --

答案 1 :(得分:1)

if defined $STATEABBRIVATE{$state}和任何哈希查找都将是恒定时间(即O(1)操作)。事实上,defined()可能在幕后使用哈希表查找。因此,我的预测是即使使用大型数据集,性能差异也可以忽略不计。这充其量只是一种有根据的猜测。