in reply to Re^2: Faster Hash Slices
in thread Faster Hash Slices

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.

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

    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'
Re^4: Faster Hash Slices
by choroba (Cardinal) on Nov 28, 2013 at 14:18 UTC
    Maybe the verification of the reference type explains the difference?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      The difference appears to be that a pad lookup requires a single opcode where dereferencing a hashref requires three opcodes:

      9 <;> nextstate(main 47 -e:1) v:%,{ ->a f <; +> nextstate(main 47 -e:1) v:%,{ ->g a <;> nextstate(main 47 -e:1) v:%,{ ->b g <; +> nextstate(main 47 -e:1) v:%,{ ->h m <2> leaveloop vK/2 ->n u <2 +> leaveloop vK/2 ->v f <{> enteriter(next->j last->m redo->g) lKS/8 ->k l + <{> enteriter(next->r last->u redo->m) lKS/8 ->s - <0> ex-pushmark s ->b - + <0> ex-pushmark s ->h - <1> ex-list lK ->e - + <1> ex-list lK ->k b <0> pushmark s ->c h + <0> pushmark s ->i c <$> const[IV 1] s ->d i + <$> const[IV 1] s ->j d <$> const[IV 10] s ->e j + <$> const[IV 10] s ->k e <#> gv[*_] s ->f k + <#> gv[*_] s ->l - <1> null vK/1 ->m - + <1> null vK/1 ->u l <|> and(other->g) vK/1 ->m t + <|> and(other->m) vK/1 ->u k <0> iter s ->l s + <0> iter s ->t - <@> lineseq vK ->- - + <@> lineseq vK ->- i <1> exists vK/1 ->j q + <1> exists vK/1 ->r - <1> ex-helem sK/2 ->i - + <1> ex-helem sK/2 ->q g <0> padhv[%h:46,47] sR ->h o + <1> rv2hv[t8] sKR/1 ->p n + <1> rv2sv sKM/DREFHV,1 ->o m + <#> gv[*r] s ->n - <1> ex-rv2sv sK/1 ->- - + <1> ex-rv2sv sK/1 ->- h <#> gvsv[*_] s ->i p + <#> gvsv[*_] s ->q j <0> unstack v ->k r + <0> unstack v ->s -e syntax OK -e synta +x OK

      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.

        I made a mistake (used a global reference); the difference is one extra opcode, not two extra:

        9 <;> nextstate(main 47 -e:1) v:%,{ ->a f <;> ne +xtstate(main 48 -e:1) v:%,{ ->g a <;> nextstate(main 47 -e:1) v:%,{ ->b g <;> ne +xtstate(main 48 -e:1) v:%,{ ->h m <2> leaveloop vK/2 ->n t <2> le +aveloop vK/2 ->u f <{> enteriter(next->j last->m redo->g) lKS/8 ->k l <{> + enteriter(next->q last->t redo->m) lKS/8 ->r - <0> ex-pushmark s ->b - +<0> ex-pushmark s ->h - <1> ex-list lK ->e - +<1> ex-list lK ->k b <0> pushmark s ->c h + <0> pushmark s ->i c <$> const[IV 1] s ->d i + <$> const[IV 1] s ->j d <$> const[IV 10] s ->e j + <$> const[IV 10] s ->k e <#> gv[*_] s ->f k +<#> gv[*_] s ->l - <1> null vK/1 ->m - <1> + null vK/1 ->t l <|> and(other->g) vK/1 ->m s +<|> and(other->m) vK/1 ->t k <0> iter s ->l r + <0> iter s ->s - <@> lineseq vK ->- - + <@> lineseq vK ->- i <1> exists vK/1 ->j p + <1> exists vK/1 ->q - <1> ex-helem sK/2 ->i - + <1> ex-helem sK/2 ->p n + <1> rv2hv[t7] sKR/1 ->o g <0> padhv[%h:46,47] sR ->h m + <0> padsv[$r:47,48] sM/DREFHV ->n - <1> ex-rv2sv sK/1 ->- - + <1> ex-rv2sv sK/1 ->- h <#> gvsv[*_] s ->i o + <#> gvsv[*_] s ->p j <0> unstack v ->k q + <0> unstack v ->r -e syntax OK e syntax OK

        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.