Performance implications of using the Switch module

From: GreenLight (google_at_milbaugh.com)
Date: 04/30/04


Date: 30 Apr 2004 11:05:33 -0700

I hate looking at rows and rows of elsif statements as much as the
next person, but using the Switch module just doesn't cut it. I wrote:

use strict;
use warnings;
use Benchmark;
use Switch;

sub use_if() {
        my ($tag, $value);
        my @parsed;
        my @tags = (
                        "TAG001^VALUE001", "TAG002^VALUE002", "TAG003^VALUE003",
"TAG004^VALUE004", "TAG005^VALUE005",
                        "TAG006^VALUE006", "TAG007^VALUE007", "TAG008^VALUE008",
"TAG009^VALUE009", "TAG010^VALUE010"
                );
        foreach my $next (@tags) {
                ($tag, $value) = ($next =~ /^(.*?)\^(.*?)$/);
                
                if ($tag eq 'TAG001') { push @parsed, $value }
                elsif ($tag eq 'TAG002') { push @parsed, $value }
                elsif ($tag eq 'TAG003') { push @parsed, $value }
                elsif ($tag eq 'TAG004') { push @parsed, $value }
                elsif ($tag eq 'TAG005') { push @parsed, $value }
                elsif ($tag eq 'TAG006') { push @parsed, $value }
                elsif ($tag eq 'TAG007') { push @parsed, $value }
                elsif ($tag eq 'TAG008') { push @parsed, $value }
                elsif ($tag eq 'TAG009') { push @parsed, $value }
                elsif ($tag eq 'TAG010') { push @parsed, $value }
                else { die "Bad tag!" }
        }
}

sub use_switch() {
        my ($tag, $value);
        my @parsed;
        my @tags = (
                        "TAG001^VALUE001", "TAG002^VALUE002", "TAG003^VALUE003",
"TAG004^VALUE004", "TAG005^VALUE005",
                        "TAG006^VALUE006", "TAG007^VALUE007", "TAG008^VALUE008",
"TAG009^VALUE009", "TAG010^VALUE010"
                );
        foreach my $next (@tags) {
                ($tag, $value) = ($next =~ /^(.*?)\^(.*?)$/);
                
                switch ($tag) {
                        case "TAG001" { push @parsed, $value }
                        case "TAG002" { push @parsed, $value }
                        case "TAG003" { push @parsed, $value }
                        case "TAG004" { push @parsed, $value }
                        case "TAG005" { push @parsed, $value }
                        case "TAG006" { push @parsed, $value }
                        case "TAG007" { push @parsed, $value }
                        case "TAG008" { push @parsed, $value }
                        case "TAG009" { push @parsed, $value }
                        case "TAG010" { push @parsed, $value }
                        else { die "Bad tag!" }
                }
        }
}

timethese (100000, {
                "Using 'if'" => \&use_if,
                "Using 'switch'" => \&use_switch
        });

__END__

These subroutines adequately represent tasks performed thousands of
times per day at my client's site.
And the results:

Benchmark: timing 100000 iterations of Using 'if', Using 'switch'...
Using 'if': 17 wallclock secs (16.48 usr + 0.00 sys = 16.48 CPU) @
6066.49/s (n=100000)
Using 'switch': 153 wallclock secs (150.68 usr + 0.00 sys = 150.68
CPU) @ 663.68/s (n=100000)

Using "switch" was nearly an order of magnitude slower.

Now my real question: Does anyone know if the "forthcoming" Perl6
version (given/when, as described in "perldoc switch") will offer
better performance (that is, is anyone actually using any early
release and can comment upon the performance)?



Relevant Pages

  • Re: Adding a base port
    ... > Adding a sub woofer may be a possibility. ... > decoded signals from the DVD player to the amp). ... > speaker outputs of the Sony, using the "large speaker" setting in the DVD ... One double-pole double-throw switch. ...
    (uk.rec.audio)
  • Re: Adding a base port
    ... You know what, I knew I'd spelled bass wrong, and haven't a clue why! ... >> Adding a sub woofer may be a possibility. ... >> decoded signals from the DVD player to the amp). ... One double-pole double-throw switch. ...
    (uk.rec.audio)
  • Re: copy files between drives from within access 2003
    ... I'm getting no error messages now. ... isn't working - just a case of adding a switch ot two for xcopy. ... Private Sub Command1_Click ... I havn't added the /d switch at the moment but added the /e switch (I ...
    (microsoft.public.access.modulesdaovba)
  • Re: Using command buttons change the value in a form text box.
    ... current status of the hours (machine or labour). ... You could have one button and switch the caption ... Private Sub LabourHours_Click ... Labour Hours command button, likewise the operator can change the ...
    (microsoft.public.excel.programming)
  • Re: error with -T taint checking
    ... > On Mon, 11 Jul 2005, Ron Smith wrote: ... >> sub Store_Results{ ... >> print STORAGE $data; ... I still get the error with the -T switch though, ...
    (perl.beginners)