Changing if to switch case in Perl

I wrote the following code using if else statements.

If I change the above statements to switch cases will it be faster or will they be the same speed. If change to switch will make it faster please advise how to do it.
if ($key eq "Id"){
print "i m inside id\n";
$data1 = "index.cfm?fuseaction=display&Instance=2&ssrid=x44249b495ca194b695506cd6305c4b4d";
}
elsif ($key eq "Inf"){
$data1 = "index.cfm?fuseaction=display&Instance=2&ssrid=xb76de7b59f8b0d3fc600314145cd7ca7";
}
elsif ($key eq "Purp"){
$data1 = "index.cfm?fuseaction=display&Instance=2&ssrid=x6a7ac11fb39acc7cf7f722134aff070b";
}
else{
my $content1 = get("http://www.iso20022.org/standardsrepository/index.cfm?FuseAction=list&Instance=2&NameOpt=contains&view=e&Name=$key");
$content1 =~ /<A href="(.*)">\&lt;$key\&gt;\<\/a\>/;
$data1=$1;
}

Open in new window

saibskAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FishMongerCommented:
I won't use either of your approaches.  Instead I'd use a hash where the keys are 'Id', Inf', and 'Purp" and their values are the encrypted strings.
my %hash = (
            Id => '2&ssrid=x44249b495ca194b695506cd6305c4b4d',
            Inf => '2&ssrid=xb76de7b59f8b0d3fc600314145cd7ca7',
            Purp => '2&ssrid=x6a7ac11fb39acc7cf7f722134aff070b',
           );
 
$data = "index.cfm?fuseaction=display&Instance=2&ssrid=$hash{$key}";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FishMongerCommented:
To answer your question more directly, using a switch statement would be the same as the if/elsif/else statement.  The advantage of it is that it's a cleaner syntax.

Using the hash would be faster and cleaner that wither of the others.
0
Adam314Commented:
I ran some tests, and found switch to be slower by ~25-30 times.  Running through that code 10,000 times:
if    : 0.016600
switch: 0.440983
if    : 0.019740
switch: 0.460576
if    : 0.017352
switch: 0.450084
if    : 0.016447
switch: 0.469646

The hash method that FishMonger suggests would be much easier to maintain.  To account for the "else", you could do (this borrows heavily from FishMonger):
my %hash = (
            Id => '2&ssrid=x44249b495ca194b695506cd6305c4b4d',
            Inf => '2&ssrid=xb76de7b59f8b0d3fc600314145cd7ca7',
            Purp => '2&ssrid=x6a7ac11fb39acc7cf7f722134aff070b',
           );
 
if(exists($hash{$key})) {
    $data1 = $hash{$key};
}
else {
    my $content1 = get("http://www.iso20022.org/standardsrepository/index.cfm?FuseAction=list&Instance=2&NameOpt=contains&view=e&Name=$key");
    $content1 =~ /<A href="(.*)">\&lt;$key\&gt;\<\/a\>/;
    $data1=$1;
}
$data = "index.cfm?fuseaction=display&Instance=2&ssrid=$data1";

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

FishMongerCommented:
"I ran some tests, and found switch to be slower by ~25-30 times."

That surprises me, I would have sworn that they would be almost the same, but obliquely not since the switch statement has the overhead of calling the sub ans its if/elsif/else blocks are more complex.
0
FishMongerCommented:
Humm, the perils of using a spell checker with my eyes half closed. :)

s/obliquely/obviously/
0
Adam314Commented:
I ran the tests on just one machine, which happened to be a windows machine.  I ran the comparisons 4 times (in case the os did something in the background that would affect the results).  The times look pretty consistent - but I don't know that the switch will always be slower.  If you run the same test, post your results.

Note: When I was running the tests, I randomly selected a key out of (Id, Inf, Purp), never running the else block...


use Time::HiRes 'time';
my @Keys = (qw(Id Inf Purp));
for(1..10000) {Using_if($Keys[rand(3)]);};
for(1..10000) {Using_switch($Keys[rand(3)]);};

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.