Advantage of 'one dimensional' hash over array in Perl -


i wondering efficiency of using 1 dimensional hash (i.e. keys, no values - don't care them anyway) on 1 dimensional array.

the main reason wanted use hash purpose can use exists function see if 'entry' exists. hashes great not duplicating keys right? arrays, need set own checks involving grep i'm led believe slower.

this hash/array iterated through operations.

i'd love hear insight on this, , in advance!

exists $hash{ $key }  

is nice, short expression, clear , easy use.

!!grep { $_ eq $key } @array 

is not quite short,

$key ~~ @array # smart match 

is shorter. since 5.10, syntactically easy test smart match exists.

so guessing performance difference between arrays , hashes, can imagine smart match perform faster small list of items, hash by far outperform array lookup large list of items.

however, should benchmark performance anyway.


and why. on strawberry perl, list size 1, hash look-up outperforms string match:

array_lookup  577701/s           --         -46% hash_lookup  1068376/s          85%           -- 

with 2 items in list:

array_lookup  464684/s           --         -57% hash_lookup  1068376/s         130%           -- 

and 20 items:

array_lookup  181554/s           --         -83% hash_lookup  1068376/s         488%           -- 

i use hash.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -