in reply to Faster Hash Slices

Switching from a hashref to a hash may slightly improve matters. Accesses to hash elements are slightly faster going direct. Better still would be to abandon the hash altogether and access @aa directly, because array accesses are faster than hash ones, but this change might not be as simple.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^2: Faster Hash Slices
by shmem (Chancellor) on Nov 28, 2013 at 13:38 UTC

    Sure? AFAIK lookups against a named hash are slower*, since they need a extra lookup against the symbol table or pad to locate its reference.

    * the speed penalty is close to zero

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      Seems to be the case that named hash lookups are (marginally, but consistently) quicker:

      { my $ref = { 1 .. 1e6 }; my %h = 1 .. 1e6; cmpthese -5,{ a => sub{ exists $ref->{$_} and 1 for 1 .. 1e6; }, b => sub{ exists $h{$_} and 1 for 1 .. 1e6; } } };; Rate a b a 1.24/s -- -3% b 1.28/s 3% -- Rate a b a 1.36/s -- -5% b 1.43/s 5% -- Rate a b a 1.24/s -- -3% b 1.28/s 3% -- Rate a b a 1.23/s -- -5% b 1.29/s 5% -- Rate a b a 1.27/s -- -3% b 1.30/s 3% --

      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.

        The same seems to be true for package variables ( scalar holding hashref and hash in the symbol table), and my variable lookup seems to be faster:

        our $ref = { 1 .. 1e6 }; our %h = 1 .. 1e6; { my $_ref = { 1 .. 1e6 }; my %_h = 1 .. 1e6; cmpthese -5,{ a => sub{ exists $_ref->{$_} and 1 for 1 .. 1e6; }, b => sub{ exists $_h{$_} and 1 for 1 .. 1e6; }, c => sub{ exists $ref->{$_} and 1 for 1 .. 1e6; }, d => sub{ exists $h{$_} and 1 for 1 .. 1e6; } } };; Rate a c d b a 3.31/s -- -2% -3% -4% c 3.38/s 2% -- -0% -2% d 3.39/s 3% 0% -- -2% b 3.45/s 4% 2% 2% --

        Thanks for measurement and correction.

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        Maybe the verification of the reference type explains the difference?
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      The reference is a named variable though, so that also needs a lookup in the symbol table or pad, plus needs to then be dereferenced.

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name