[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Changing if to switch case in Perl

Posted on 2008-01-25
6
Medium Priority
?
3,924 Views
Last Modified: 2008-02-26
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

0
Comment
Question by:saibsk
  • 4
  • 2
6 Comments
 
LVL 28

Accepted Solution

by:
FishMonger earned 1000 total points
ID: 20744618
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
 
LVL 28

Expert Comment

by:FishMonger
ID: 20744688
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
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 1000 total points
ID: 20744734
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 28

Expert Comment

by:FishMonger
ID: 20744878
"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
 
LVL 28

Expert Comment

by:FishMonger
ID: 20744920
Humm, the perils of using a spell checker with my eyes half closed. :)

s/obliquely/obviously/
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20745290
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question