Perl array vs. hash handing over Benchmark

I want to compare the hash and array in Perl and both of them with a reference and a normal handing over.That is the code, I tried to hold it as simple and fast as possible:

#!/usr/bin/perl
use strict;
use Benchmark qw(:all) ;
my @arr = (1..4);
my %hash;
@hash{(1..4)}=(1..4);
 
 
print ref_hash(\%hash);
print normal_hash(%hash);
print proto_hash(%hash);
print ref_arr(\@arr);
print normal_arr(@arr);
print proto_arr(@arr);
 
 
cmpthese(-4, {
'ref_hash'     => sub {ref_hash(\%hash)},
'normal_hash'  => sub {normal_hash(%hash)},
'proto_hash'   => sub {proto_hash(%hash)},
'ref_arr'      => sub {ref_arr(\@arr)},
'normal_arr'   => sub {normal_arr(@arr)},
'proto_arr'    => sub {proto_arr(@arr)},
});
#hash reference
sub ref_hash {
    my $out;
    for (keys %{$_[0]}){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}
#normal hash
sub normal_hash {
    my $out;
    for (keys {@_}){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}
#prototype hash
sub proto_hash(\%) {
    my $out;
    for (keys {@_}){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}
#refernce array
sub ref_arr {
    my $out;
    for(@{$_[0]}){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}
#normal array
sub normal_arr {
    my $out;
    for(@_){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}
#prototype array
sub proto_arr(\@) {
    my $out;
    for(@_){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}

Our result :

4    
6    
8    
2    
     
---  
6    
4    
2    
8    
     
---  
6    
4    
8    
2    
     
---  
2    
4    
6    
8    
     
---  
2    
4    
6    
8    
     
---  
2    
4    
6    
8    
     
---

Looks good, the keys from the hashes are not sorted but I don’t want to waste performance with a sort.

 

Now this is the Benchmark result from 1..4 :

                Rate proto_hash normal_hash ref_hash ref_arr proto_arr normal_arr 
proto_hash  147908/s         --        -12%     -31%    -56%      -59%       -60% 
normal_hash 167716/s        13%          --     -22%    -50%      -54%       -54% 
ref_hash    215426/s        46%         28%       --    -35%      -40%       -41% 
ref_arr     333199/s       125%         99%      55%      --       -8%        -9% 
proto_arr   361439/s       144%        116%      68%      8%        --        -2% 
normal_arr  367775/s       149%        119%      71%     10%        2%         --

We could see the normal array is faster than the shift and prototype so for small data a reference is not faster than a normal use, but by the hash it is much faster than a normal handing over.and we could see array is faster than a hash, but the hash takes more information (key and values).

 

And this is the Benchmark result from 1..1000 :

              Rate proto_hash normal_hash ref_hash proto_arr normal_arr  ref_arr 
proto_hash  1014/s         --         -0%     -37%      -61%       -61%     -63% 
normal_hash 1019/s         0%          --     -37%      -61%       -61%     -63% 
ref_hash    1611/s        59%         58%       --      -38%       -39%     -42% 
proto_arr   2606/s       157%        156%      62%        --        -1%      -6% 
normal_arr  2623/s       159%        157%      63%        1%         --      -5% 
ref_arr     2759/s       172%        171%      71%        6%         5%       --

Now the reference from the array and hash are both fastest, the hash is always slower than the array.Just take a look at scalars.

3 thoughts on “Perl array vs. hash handing over Benchmark”

  1. Hi there! Do you know if they make any plugins to help with SEO?
    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains.
    If you know of any please share. Cheers!

  2. Thank you for sharing your info. I really appreciate your efforts and I am waiting for
    your next write ups thanks once again.

Leave a Reply

Your email address will not be published.

18 + 12 =