Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: When should I use a dispatch table?

by runrig (Abbot)
on Dec 01, 2006 at 00:04 UTC ( #587086=note: print w/ replies, xml ) Need Help??


in reply to Re: When should I use a dispatch table?
in thread When should I use a dispatch table?

Avoiding exists:

($dispatch{$item} || $dispatch{default})->()


Comment on Re^2: When should I use a dispatch table?
Download Code
Re^3: When should I use a dispatch table?
by friedo (Prior) on Dec 01, 2006 at 00:26 UTC

    runrig++, very clever. I had to stare at it for a second to realize what was going on.

Re^3: When should I use a dispatch table?
by Limbic~Region (Chancellor) on Dec 01, 2006 at 00:28 UTC
    runrig,
    The issue that traveler pointed out was that I tested if a key existed when I didn't need to. Only when the user defines the -u option is it necessary to test for the hash key's existance. Without the -u, there is no default condition and the exists can be avoided. I corrected the template accordingly.

    You have mentioned an alternative way of determining which sub to dispatch. Unfortunately it may actually be less efficient than exists. You are fetching the value and testing it for truth where exists need only check to see if the key is present in the hash.

    Cheers - L~R

      Yes, my version requires that there is a default (I hadn't examined the full version of your code closely as to whether or not it required a default), and although I know exists is more efficient than fetching the value unconditionally, overall efficiency depends on what the common case is. If most of the time you are fetching an existing value, then you may as well fetch it instead of testing for existence and then fetching it anyway. Your version always tests for existence then fetches one value or the other.

      Also, I agree w/chromatic's rant below, and the usual rant about premature optimization also applies (though I realize benchmarking this sort of thing can sometimes be a fun way to pass the time). :-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2014-09-19 20:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (145 votes), past polls