Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^4: "exists $hash{key}" is slower than "$hash{key}"

by swl (Parson)
on Jan 07, 2020 at 00:52 UTC ( [id://11111092]=note: print w/replies, xml ) Need Help??


in reply to Re^3: "exists $hash{key}" is slower than "$hash{key}"
in thread "exists $hash{key}" is slower than "$hash{key}"

Yes, I assume the code is written this way for good historical reasons. These reasons might no longer apply or be out-weighed by other factors, but I don't know enough to be able to say if any change would be worthwhile.

The advantage of exists over checking values is tangential to the main point of the post. It's about memory usage - if one re-uses the same SV across hash values then that takes less memory than using a new SV for each hash value. It's another case where one needs a very large number of hash entries for it to make a meaningful difference, but there are times when this applies.

  • Comment on Re^4: "exists $hash{key}" is slower than "$hash{key}"

Replies are listed 'Best First'.
Re^5: "exists $hash{key}" is slower than "$hash{key}"
by LanX (Saint) on Jan 07, 2020 at 01:45 UTC
    > It's about memory usage

    I can't say much about this, ...

    ... but my gut feeling is that this is a kind of micro optimization, which's efforts should rather spent else where.

    Otherwise you should talk to p5p ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Yes, it is most definitely a micro-optimisation. This is consistent with the last two paras in my original post, although I did not explicitly use that term there.

        Most of the above thread is, err, basically wrong! The two Concise outputs show that the only two ops executed in each case are the GV (to get the glob containing the global hash variable) followed by a MULTIDEREF op. The ex-helem and ex-exists are ops that were optimised away and replaced with the MULTIDEREF op (on perl 5.22.0 and later, anyway) - they are no longer on the execution path.

        The difference in performance between exists and a plain hash lookup is negligible, although exists should be slightly faster. The slowdown being seen is due to assigning a boolean return value to a scalar. Perl's boolean values (as returned by exists etc) are multi-valued scalars containing an int value of 0/1, a floating value of 0.0/1.0 and a string value of ""/"1". If this value is assigned to a scalar, all three parts need to be copied, including the string. Boolean values are optimised for use in conditionals. If you replace the assignment with something like $xx_global = exists $hash{$key2} ? 1 : 2 you'll see exists becomes faster than a hash lookup.

        Dave.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 03:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found