P is for Practical PerlMonks

### comment on

 Need Help??

A new benchmark trying to measure only the effect of the data structure being used. No need for further comments except that it is an entirely different benchmark:

```use strict;
use warnings;
use Benchmark  qw/cmpthese/;
my @translation = qw / Zero One Two Three Four Five Six/;
my %trans = (1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "
+Five", 6 => "Six");
my @dispatch = ( sub {return "Zero"}, sub {return "One"}, sub {return
+"Two"}, sub {return "Three"}, sub {return "Four"}, sub {return "Five"
+}, sub {return "Six"} );

sub test2 {
my \$var2 = shift;
return ("One")    if (\$var2 == 1 );
return ("Two")    if (\$var2 == 2 );
return ("Three")  if (\$var2 == 3 );
return ("Four ")  if (\$var2 == 4 );
return ("Five")   if (\$var2 == 5 );
return undef;
}
sub test3 {
my \$var2 = shift;
return \$translation[\$var2];
}
sub test4 {
my \$var = shift;
return \$trans{\$var} ;
}
sub test5 {
my \$var = shift;
eval { goto "_\$var" }    or
return "Other";
_1: return "One"  ;
_2: return "Two"  ;
_3: return "Three";
_4: return "Four";
_5: return "Five";
}
sub test6 {
my \$var = shift;
return \$dispatch[(\$var)]->();
}

cmpthese( -1,
{
_linear_2       => q {test2("5")},
_array          => q {test3("5")},
_hash             => q {test4("5")},
_goto           => q {test5("5")},
_dispatch       => q {test6("5")},
}
)
And the results:
```\$ perl test_if.pl
Rate     _goto _linear_2 _dispatch     _hash    _array
_goto     1247485/s        --      -17%      -26%      -50%      -52%
_linear_2 1509714/s       21%        --      -11%      -40%      -42%
_dispatch 1688450/s       35%       12%        --      -33%      -36%
_hash     2513434/s      101%       66%       49%        --       -4%
_array    2621427/s      110%       74%       55%        4%        --
Have a nice evening.

In reply to Re: Given When Syntax by Laurent_R
in thread Given When Syntax by Deep_Plaid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2019-10-14 10:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2019 the site I miss most is:

Results (36 votes). Check out past polls.

Notices?