Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: How to add more conditional statements in an efficient manner (updated x2)by haukex (Archbishop) |
on May 02, 2017 at 19:32 UTC ( [id://1189346]=note: print w/replies, xml ) | Need Help?? |
For string eq and ne, I like huck's hash table idea. For the generic problem of testing something against multiple other things with the same condition, see threads like Testing multiple variables against the same criteria (IF statement). Just one of several Ways To Do It:
Note I haven't benchmarked it*, but my feeling is this would probably be slower than hash lookups. If it is speed you're after, you might try Benchmarking different approaches. My gut feeling is that raw if ($x ne $y && $x ne $z && $x ne ... ) { and a regex (see e.g. this) might be fastest, along with hash lookups, followed by solutions using modules. But then again, don't get lost in premature optimizations either - one advantage of the example above is that the code reads very nicely :-) * Update: Interesting, hash lookup outperforms the others by a large margin (code is below):
Update 2: Inspired by Marshall's post about ne vs. !=, if you add that test to the above benchmark, it turns out that hashes still outperform it by a factor of roughly 2. Of course, numeric comparisons may not apply in your case!
In Section
Seekers of Perl Wisdom
|
|