madbombX has asked for the wisdom of the Perl Monks concerning the following question:
With all this discussion of Dispatch Tables (apparently brought forward mostly by me), I have been attempting to implement them in places I think they would make my code more maintainable and more efficient. In more than a few places, I have successfully replaced chains of if/else statements with dispatch tables and a successful speedup (sometimes of over 300% on longer chains).
However, I have come to a situation that I am not sure weather or not to leave the current code in place (which is not very clean) or attempt to replace it. The speed of the code is somewhat important as the module gets called (loaded, used, and then unloaded) every time an email comes in (which happens more than just a few thousand times per day). Since these are all integer comparisons, they should all be fast anyway, but there might just be a more elegent way of doing this.
Currently the code is made up of chained conditionals (which happens to be slightly beyond the tipping point of 12 that Limbic~Region proved in When should I use a dispatch table?. However, I don't see (probably through lack of knowledge) how one would implement a dispatch table since 2 tests are required to obtain an outcome (or $rv) and the hash can only have one key.
# data characteristics # 0.0001 <= $conf < 1.0000 # 0.0001 <= $prob <= 1.0000 my $outcome = ( (($prob-0.5)*2) + ($conf*100)); # Current code if ($outcome > 97) { $rv .= "99"; } elsif ( ($outcome > 93) && ($outcome <= 97) ) { $rv .= "95"; } elsif ( ($outcome > 85) && ($outcome <= 93) ) { $rv .= "90"; } elsif ( ($outcome > 75) && ($outcome <= 85) ) { $rv .= "80"; } elsif ( ($outcome > 65) && ($outcome <= 75) ) { $rv .= "70"; } elsif ( ($outcome > 55) && ($outcome <= 65) ) { $rv .= "60"; } elsif ( ($outcome > 45) && ($outcome <= 55) ) { $rv .= "50"; } elsif ( ($outcome > 35) && ($outcome <= 45) ) { $rv .= "40"; } elsif ( ($outcome > 25) && ($outcome <= 35) ) { $rv .= "30"; } elsif ( ($outcome > 15) && ($outcome <= 25) ) { $rv .= "20"; } elsif ( ($outcome > 7) && ($outcome <= 15) ) { $rv .= "10"; } elsif ( ($outcome > 3) && ($outcome <= 7) ) { $rv .= "05"; } elsif ( ($outcome > 0) && ($outcome <= 3) ) { $rv .= "00"; } else { return "ERROR"; }
Currently, my only 2 thought processes are to stay with the current code or change it to use Switch (which I have yet to Benchmark for my purpose). Thanks in advance. Thoughts?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Efficiency & Maintenance: if/elsif or Other Dispatch Method
by jdporter (Paladin) on Dec 01, 2006 at 18:14 UTC | |
by Herkum (Parson) on Dec 01, 2006 at 21:45 UTC | |
|
Re: Efficiency & Maintenance: if/elsif or Other Dispatch Method
by davido (Cardinal) on Dec 01, 2006 at 18:09 UTC | |
|
Re: Efficiency & Maintenance: if/elsif or Other Dispatch Method
by Not_a_Number (Prior) on Dec 01, 2006 at 18:59 UTC | |
|
Re: Efficiency & Maintenance: if/elsif or Other Dispatch Method
by BrowserUk (Patriarch) on Dec 01, 2006 at 19:18 UTC | |
|
Re: Efficiency & Maintenance: if/elsif or Other Dispatch Method
by Fletch (Bishop) on Dec 01, 2006 at 18:46 UTC | |
by Limbic~Region (Chancellor) on Dec 01, 2006 at 19:19 UTC | |
|
Re: Efficiency & Maintenance: if/elsif or Other Dispatch Method
by Limbic~Region (Chancellor) on Dec 01, 2006 at 19:27 UTC |