Monks,

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?


In reply to Efficiency & Maintenance: if/elsif or Other Dispatch Method by madbombX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.