in reply to Trinary or If'n'Else?
As for a benchmark, here's what I used:
You can play with different ways of constructing the if-else block. With 500,000 iterations, the ternary is 4% faster. I wouldn't worry about a difference like that.sub trinary { my $a = 1; my $b = 0; return $a > $b ? $a : $b; } sub if_else { my $a = 1; my $b = 0; if ($a > $b) { return $a; } else { return $b; } } use Benchmark; timethese(500000, { trinary => \&trinary, if_else => \&if_else, });
Removing the else flips it the other way:
sub if_else { my $a = 1; my $b = 0; return $a if $a > $b; return $b; }
|
|---|