Re: initialize a hash



Paul Lalli schreef:
Dr.Ruud:

I am investigating what is the fastest way to set up a hash, with the
keys coming from an array, and all values set to a constant like 1.

x1s => sub { my %a ; @a{ @list } = (1) x @list },

Yes, that's more what I am looking for. Thanks, Uri and Anno too of
course.

My 'list' and 'range' (and 'empty') were meant as speed references; they
don't set all values to the same (and defined) constant.


Rate mapB mapE list for x1s range empty
mapB 243/s -- -0% -9% -41% -48% -48% -62%
mapE 243/s 0% -- -8% -41% -48% -48% -62%
list 266/s 10% 9% -- -36% -43% -43% -58%
for 412/s 70% 69% 55% -- -11% -12% -35%
x1s 464/s 91% 90% 74% 12% -- -1% -27%
range 467/s 93% 92% 76% 13% 1% -- -27%
empty 638/s 163% 162% 140% 55% 38% 37% --


On your system, 'list' isn't as near to 'for' as on the freebsd with
perl 5.8.6 here.

I still wish there were a construct that would deliver as many
<constant>s as requested.
Something like C<1...undef> maybe, but no: that would break existing
code.


This one is (just 1 or 2%) faster than "empty":

empty1 => sub { my %a ; @a{ @list } = undef },

--
Affijn, Ruud

"Gewoon is een tijger."


.



Relevant Pages

  • Re: ordered hashes
    ... MV> array with the order of the keys, rather than using an ordered hash. ... show me a better version line in any other lang. ...
    (comp.lang.perl.misc)
  • Re: random array elements and speed
    ... I suppose the fact that both you and Uri gave rather similar ... JJ> incredibly sensitive to the size of the desired array. ... JJ> then yeah the splice method might well be two or three or four times as ... But the random selection method could be a ...
    (comp.lang.perl.misc)
  • Re: comparing some but not all fields in lists
    ... and what's in common." ... I should compare only 3 of the 82 fields in each list. ... comparing all 82 fields as a single array element. ... cannot handle multiple keys as you want it to. ...
    (perl.beginners)
  • Re: Build an array from a text file
    ... Now that I have created my array, ... The dictionary object has a 'keys' method that returns a 0 based array ... Dim vArray As Variant ... dictionaries, so any help would be great! ...
    (microsoft.public.word.vba.general)
  • Re: Nugget - Using control references
    ... until someone decides they want to save a 3D array. ... If the users are likely to add elements to the middle of the enum, you should save and load by the enum text. ... What you can try is this - save both the string and the number. ... Unused keys are not deleted.  When the number of elements in an array is reduced, the key entries still exist in the file. ...
    (comp.lang.labview)