Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Regarding speed: Is elsif or just if faster?

by davidj (Priest)
on Feb 25, 2007 at 01:04 UTC ( #601948=note: print w/ replies, xml ) Need Help??


in reply to Regarding speed: Is elsif or just if faster?

I had a situation once where I needed to switch on 14 or 15 possible variable values. It made more sense to me to to write a hash of function refs. I never benchmarked it for speed, but I sure found it much easier to maintain over the long term, especially as the requirements for each possible variable changed. Something like this:

#!/usr/bin/perl $func_refs = { '1' => \&f1, '2' => \&f2, '3' => \&f3 }; &{ $func_refs->{$arg} }; sub f1() { #do something } sub f2() { # do something } .......
davidj


Comment on Re: Regarding speed: Is elsif or just if faster?
Download Code
Re^2: Regarding speed: Is elsif or just if faster?
by blogical (Pilgrim) on Feb 25, 2007 at 03:16 UTC
    I agree- dispatch table ahoy! Maybe not for three possible inputs, but it sounds like this was inspired by a greater number, or by large branched chunks of code obscuring the elsif switches.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-09-16 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (5 votes), past polls