Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
That's a good point - the dispatch table is created on every invocation, the elsif code variant is only compiled once. The second point is that the split calls actually obscure the test results, not clarify them.

So what I did was to change all the alternatives to return a literal 'A' instead, and to build the dispatch table only once. That's the result:

Rate elsif dispatch elsif 252062/s -- -73% dispatch 927942/s 268% --
And that's the code I used:
#!/usr/bin/perl # elsifbench - which is faster, a chain of elsif or a dispatch table? # SSF 112208 042609 use strict; use Benchmark qw/:all/; sub random { int rand shift } my @letters=('A'..'Z','a'..'z','0'..'9'); my $nLetters=scalar @letters; sub elsifchain { my $x=$letters[random($nLetters)]; if ($x eq 'A') { 'A' } elsif ($x eq 'B') { 'A' } elsif ($x eq 'C') { 'A' } elsif ($x eq 'D') { 'A' } elsif ($x eq 'E') { 'A' } elsif ($x eq 'F') { 'A' } elsif ($x eq 'G') { 'A' } elsif ($x eq 'H') { 'A' } elsif ($x eq 'I') { 'A' } elsif ($x eq 'J') { 'A' } elsif ($x eq 'K') { 'A' } elsif ($x eq 'L') { 'A' } elsif ($x eq 'M') { 'A' } elsif ($x eq 'N') { 'A' } elsif ($x eq 'O') { 'A' } elsif ($x eq 'P') { 'A' } elsif ($x eq 'Q') { 'A' } elsif ($x eq 'R') { 'A' } elsif ($x eq 'S') { 'A' } elsif ($x eq 'T') { 'A'; } elsif ($x eq 'U') { 'A'; } elsif ($x eq 'V') { 'A'; } elsif ($x eq 'W') { 'A'; } elsif ($x eq 'X') { 'A'; } elsif ($x eq 'Y') { 'A'; } elsif ($x eq 'Z') { 'A'; } elsif ($x eq 'a') { 'A'; } elsif ($x eq 'b') { 'A'; } elsif ($x eq 'c') { 'A'; } elsif ($x eq 'd') { 'A'; } elsif ($x eq 'e') { 'A'; } elsif ($x eq 'f') { 'A'; } elsif ($x eq 'g') { 'A'; } elsif ($x eq 'h') { 'A'; } elsif ($x eq 'i') { 'A'; } elsif ($x eq 'j') { 'A'; } elsif ($x eq 'k') { 'A'; } elsif ($x eq 'l') { 'A'; } elsif ($x eq 'm') { 'A'; } elsif ($x eq 'n') { 'A'; } elsif ($x eq 'o') { 'A'; } elsif ($x eq 'p') { 'A'; } elsif ($x eq 'q') { 'A'; } elsif ($x eq 'r') { 'A'; } elsif ($x eq 's') { 'A'; } elsif ($x eq 't') { 'A'; } elsif ($x eq 'u') { 'A'; } elsif ($x eq 'v') { 'A'; } elsif ($x eq 'w') { 'A'; } elsif ($x eq 'x') { 'A'; } elsif ($x eq 'y') { 'A'; } elsif ($x eq 'z') { 'A'; } elsif ($x eq '0') { 'A'; } elsif ($x eq '1') { 'A'; } elsif ($x eq '2') { 'A'; } elsif ($x eq '3') { 'A'; } elsif ($x eq '4') { 'A'; } elsif ($x eq '5') { 'A'; } elsif ($x eq '6') { 'A'; } elsif ($x eq '7') { 'A'; } elsif ($x eq '8') { 'A'; } elsif ($x eq '9') { 'A'; } else { warn "Huh?"; } } my %dispatch; for ('a'..'z', 'A'..'Z', 0..9) { $dispatch{$_} = sub { 'A' }; } sub dispatch { my $x=$letters[random($nLetters)]; if (exists $dispatch{$x}) { $dispatch{$x}->(); } else { warn "Huh?"; } } cmpthese(-1,{elsif=>\&elsifchain,dispatch=>\&dispatch}); exit;

In reply to Re^2: elsif chain vs. dispatch by moritz
in thread elsif chain vs. dispatch by sflitman

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2014-12-27 10:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (177 votes), past polls