$x = 5;
my @arr = ('zero', 'one', 'two', 'three');
my %hash = (
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
);
...
cmpthese(-1, +{
'array lookup' => sub {
my $y = $arr[$x] // 'too many';
},
'hash lookup' => sub {
my $y = $hash{$x} // 'too many';
},
...
});
perl bench_given_when.pl
Rate switch sub return if ... {} given and or elsif && || ?: hash lookup array lookup
switch 138400/s -- -92% -95% -98% -99% -99% -99% -99% -99% -100%
sub return if ... 1820444/s 1215% -- -40% -75% -85% -85% -86% -88% -92% -94%
{} 3054952/s 2107% 68% -- -58% -75% -75% -77% -80% -87% -90%
given 7340032/s 5203% 303% 140% -- -39% -41% -44% -53% -69% -75%
and or 11993516/s 8566% 559% 293% 63% -- -4% -8% -23% -49% -59%
elsif 12461337/s 8904% 585% 308% 70% 4% -- -5% -20% -47% -58%
&& || 13107200/s 9371% 620% 329% 79% 9% 5% -- -16% -45% -55%
?: 15534459/s 11124% 753% 409% 112% 30% 25% 19% -- -34% -47%
hash lookup 23644360/s 16984% 1199% 674% 222% 97% 90% 80% 52% -- -20%
array lookup 29433712/s 21167% 1517% 863% 301% 145% 136% 125% 89% 24% --
Какая версия perl?
Обсуждают сегодня