Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Faster Hash Slices

by shmem (Chancellor)
on Nov 28, 2013 at 13:38 UTC ( [id://1064826]=note: print w/replies, xml ) Need Help??


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

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'

Replies are listed 'Best First'.
Re^3: Faster Hash Slices
by BrowserUk (Patriarch) on Nov 28, 2013 at 14:02 UTC

    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 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.
Re^3: Faster Hash Slices
by tobyink (Canon) on Nov 28, 2013 at 14:23 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1064826]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (9)
As of 2024-04-23 18:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found