bradcathey has asked for the wisdom of the Perl Monks concerning the following question:
but in reading Prog. Perl (p. 125) I see this could send me to the confessional. So, I tried:if ($x == 1) { $abc = 1 } elsif ($x == 2) { $def = 1 } elsif ($x == 3) { $xyz = 1 } else { $nothing = 1 }
orSWITCH: { if ($x == 1) { $abc = 1; last SWITCH; }; if ($x == 2) { $def = 1; last SWITCH; }; if ($x == 3) { $xyz = 1; last SWITCH; }; $nothing = 1; }
Sure it looks nicer, but in speed tests, depending on how $x is set, this later structure always slower, and in some cases significantly.SWITCH: for ($x) { if (1) { $abc = 1; last SWITCH; }; if (2) { $def = 1; last SWITCH; }; if (3) { $xyz = 1; last SWITCH; }; $nothing = 1; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Use of SWITCH vs IF ELSE in speed and readability
by CombatSquirrel (Hermit) on Aug 30, 2003 at 16:07 UTC | |
by bradcathey (Prior) on Aug 30, 2003 at 17:35 UTC | |
by CombatSquirrel (Hermit) on Aug 31, 2003 at 00:10 UTC | |
|
Re: Use of SWITCH vs IF ELSE in speed and readability
by Zaxo (Archbishop) on Aug 30, 2003 at 16:29 UTC | |
by halley (Prior) on Sep 01, 2003 at 03:37 UTC | |
|
Re: Use of SWITCH vs IF ELSE in speed and readability
by perrin (Chancellor) on Aug 30, 2003 at 18:06 UTC | |
|
Re: Use of SWITCH vs IF ELSE in speed and readability
by BrowserUk (Patriarch) on Aug 31, 2003 at 08:03 UTC | |
|
Re: Use of SWITCH vs IF ELSE in speed and readability
by TomDLux (Vicar) on Aug 30, 2003 at 18:28 UTC |