in reply to '100 elsif's vs hash of coderef's
To answer the question you asked; the breakpoint for performance comes around the 10 cases mark:
#! perl -slw use strict; use List::Util qw[ shuffle ]; use Benchmark qw[ cmpthese ]; our %dispatch = ( 'cybox:abcdef' => sub { 1; }, 'cybox:ghijkl' => sub { 1; }, 'cybox:mnopqr' => sub { 1; }, 'cybox:stuvwx' => sub { 1; }, 'cybox:123456' => sub { 1; }, 'cybox:234567' => sub { 1; }, 'cybox:345678' => sub { 1; }, 'cybox:456789' => sub { 1; }, 'cybox:567890' => sub { 1; }, 'cybox:000000' => sub { 1; }, ); our @k = map{ shuffle keys %dispatch } 1 .. 100; cmpthese -1, { a=>q[ for my $tname ( @k ) { $dispatch{ $tname }->(); } ], b=>q[ for my $tname ( @k ) { if( $tname eq 'cybox:abcdef' ) { 1; } elsif( $tname eq 'cybox:ghijkl' ) { 1; } elsif( $tname eq 'cybox:mnopqr' ) { 1; } elsif( $tname eq 'cybox:stuvwx' ) { 1; } elsif( $tname eq 'cybox:123456' ) { 1; } elsif( $tname eq 'cybox:234567' ) { 1; } elsif( $tname eq 'cybox:345678' ) { 1; } elsif( $tname eq 'cybox:456789' ) { 1; } elsif( $tname eq 'cybox:567890' ) { 1; } else { 1; } } ], }; __END__ C:\test>1149900 Rate b a b 1810/s -- -8% a 1967/s 9% -- C:\test>\Perl22\bin\perl.exe 1149900.pl Rate b a b 2370/s -- -18% a 2899/s 22% --
Though personally, I tend to gravitate to a dispatch hash when I get 4 or 5 cases or more because it makes for cleaner code.
|
|---|