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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
#!/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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
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 :
1 2 3 4 5 6 7 |
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 :
1 2 3 4 5 6 7 |
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.
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!
Thank you for sharing your info. I really appreciate your efforts and I am waiting for
your next write ups thanks once again.
Incredible points. Outstanding arguments. Keep up to date the great work.