in reply to Faster Hash Slices

Under the assumption that @head stays constant during the loop, do you declare the hash outside of the loop? As the keys of the hash are same every iteration, it would be wasteful, to create and delete the hash structure, allocate memory for the keys, etc every time. So it should be similar to this (leaving chomping aside for the moment):

my @head = split /\t/, <$fh>; my $ref = {}; undef @{$ref}{@head}; # pre-allocate the hash and its keys for( <$fh> ) { @{$ref}{@head} = split /\t/; # do something with the data here }

Replies are listed 'Best First'.
Re^2: Faster Hash Slices
by jht (Novice) on Nov 29, 2013 at 02:44 UTC
    This helped. Thanks!

      If you'd show a little more of what you do with the hash within the loop; I'm betting there is more to be gained.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.