Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: elsif chain vs. dispatch

by moritz (Cardinal)
on Apr 26, 2009 at 22:28 UTC ( #760208=note: print w/ replies, xml ) Need Help??


in reply to Re: elsif chain vs. dispatch
in thread elsif chain vs. dispatch

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;


Comment on Re^2: elsif chain vs. dispatch
Select or Download Code
Re^3: elsif chain vs. dispatch
by sflitman (Hermit) on Apr 26, 2009 at 23:46 UTC
    I knew I was doing something dumb. Thanks!

    SSF

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://760208]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-09-20 02:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls