Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
There's more than one way to do things
 
PerlMonks  

Re: Trinary or If'n'Else?

by chromatic (Archbishop)
on Dec 22, 2000 at 01:11 UTC ( [id://47986]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Trinary or If'n'Else?

Run perl -MO=Deparse on your code to see what it looks like after optimization. On Perl 5.5.3 on my box, it doesn't change it.

As for a benchmark, here's what I used:

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, });
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.

Removing the else flips it the other way:

sub if_else { my $a = 1; my $b = 0; return $a if $a > $b; return $b; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://47986]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.