use strict; use Benchmark; # The Declarative part has been take out of the loop my %switch; %switch = ( 1=> sub{print STDERR '1'}, 2=> sub{print STDERR '2'}, 3=> sub{print STDERR '3'}, 4=> sub{print STDERR '4'}, 5=> sub{print STDERR '5 or 6'}, 6=> sub {$switch{5}()}, fred=> sub{print STDERR 'fred'}, __DEFAULT__=>sub{print STDERR 'default'}, ); my @loopList; for (my $count=10; $count < 100; $count +=10){ @loopList = (1..$count, 'fred'); printf "\n====== Benchmark for %.1f%% match rate (Count= %3d)===\n" , 7 * 100 / ($count + 1), $count; RunTest(); } ########################## sub RunTest{ timethese( 10**4 ,{ switch => sub { sub switch{ eval{ goto "case_$_[0]" } or goto default; } for my $expr ( @loopList ) { switch( $expr ); { case_1: print STDERR '1'; last; case_2: print STDERR '2'; last; case_3: print STDERR '3'; last; case_4: print STDERR '4'; last; case_5: ; case_6: print STDERR '5 or 6'; last; case_fred: print STDERR 'fred'; last; default: print STDERR "default"; } } }, #/switch=>sub if => sub { for my $expr ( @loopList ) { if ($expr eq '1') {print STDERR '1'} elsif ($expr eq '2') {print STDERR '2'} elsif ($expr eq '3') {print STDERR '3'} elsif ($expr eq '4') {print STDERR '4'} elsif ($expr eq '5' or $expr eq '6') {print STDERR '5 or 6'} elsif ($expr eq 'fred') {print STDERR 'fred'} else {print STDERR "default"} } },#/if=>sub HashSub => sub{ for my $expr ( @loopList ) { #print "Iter=$expr;\n"; if( exists $switch{$expr}){ $switch{$expr}() }else{ $switch{__DEFAULT__}(); }; }; #/for },#/UseHash } ); #/TimeThese }#/RunTest