in reply to Performance - Re: Re: Re: Simple Switch statement
in thread Simple Switch statement
$ cat switch.pl #!/usr/local/bin/perl use Switch; my $letter = "c"; switch($letter) { case "a" {print "1"} case "b" {print "2"} case "c" {print "3"} }
Profiling this program with Devel::NYTProf, gave some valuable insight:
Though my program had 8 lines, a total of 5188 statements were processed!______Source_Code_Files_—_ordered_by_exclusive_time_then_name__ +_____ |Stmts|Exclusive|Reports |Source File + | |_____|Time_____|__________________|__________________________________ +_____| |3508_|324ms____|line • block |Text/Balanced.pm_______________________ +| |542__|147ms____|line • block |Switch.pm______________________________ +| |77___|32.1ms___|line • block |Exporter/Heavy.pm______________________ +| |52___|31.7ms___|line • block |Config_heavy.pl________________________ +| |132__|30.9ms___|line • block |warnings/register.pm___________________ +| |73___|22.2ms___|line • block |DynaLoader.pm__________________________ +| |39___|29.2ms___|line • block |SelfLoader.pm_(including_1_string_eval) +| |27___|15.5ms___|line • block |AutoLoader.pm__________________________ +| |184__|13.3ms___|line • block |overload.pm____________________________ +| |83___|12.9ms___|line • block |vars.pm________________________________ +| |170__|12.2ms___|line • block |Exporter.pm____________________________ +| |40___|10.4ms___|line • block |Config.pm______________________________ +| |12___|9.38ms___|line • block |Carp.pm________________________________ +| |25___|7.80ms___|line • block |Filter/Util/Call.pm____________________ +| |15___|7.77ms___|line • block |switch.pl______________________________ +| |25___|6.78ms___|line • block |version.pm_____________________________ +| |123__|4.36ms___|line • block |strict.pm______________________________ +| |34___|2.22ms___|line • block |warnings.pm____________________________ +| |26___|619µs___|line • block |feature.pm_____________________________| |1____|8µs_____|line • block |Config_git.pl__________________________| |5188_|720ms____|Total_(-4_statements_are_unaccounted_for)____________ +_____| |259__|36.0ms___|Average______________________________________________ +_____| |_____|12.9ms___|Median_______________________________________________ +_____| |_____|0.00852__|Deviation____________________________________________ +_____| Report produced by the NYTProf_4.04 Perl profiler, developed by Tim_Bu +nce and Adam_Kaplan.
Profiler output is:$ cat if-then-else.pl #!/usr/local/bin/perl my $letter = "c"; if ($letter eq "a") {print "1"; }elsif ($letter eq "b") {print "2"; }elsif ($letter eq "c") {print "3"; }
____________________then_name_____________________ |Stmts|Exclusive|Reports |Source File | |_____|Time_____|__________________|_______________| |3____|13.0ms___|line • block |if-then-else.pl|
Much better!
And the profiler output:#!/usr/local/bin/perl my $letter = "c"; my %h = ( "a" => sub{print "1"}, "b" => sub{print "2"}, "c" => sub{print "3"}, ); $h{$letter}->();
Source Code Files — ordered by exclusive time _____________________then_name_____________________ |Stmts|Exclusive|Reports |Source File | |_____|Time_____|__________________|________________| |5____|4.10ms___|line • block |dispatch-hash.pl|
The best!!
Of course, plain if-then-else and dispatch-hash are comparable. Depending on the use case, either may be chosen. However, it is clear that both of them are way better than Switch. For an in-production CGI application that I was tasked to improve performance of, replacing Switch with if-then-else gave a 150% improvement in performance. Many thanks to Tim Bunce and Adam Kaplan for writing that very useful tool - Devel::NYTProf.
|
|---|