in reply to if-else vs. tertiary effieciency?

First of all, i'd write:

print $var ? 'yes' : 'no'

btw. it's a 'ternary' operator. I cannot agree with your results, my perl (5.6.1 and 5.8.0) lets ?: be only about 13% faster:

use Benchmark qw/cmpthese/; our @values = (1,0,undef,50,'yes','no','0E0',0e0,0); my $subs = { ifelse => sub {foreach(@values){ my $x; if($_){$x = 'yes'} else {$x = 'no'} }}, trinary => sub {foreach(@values){ my $x; $x = $_ ? 'yes' : 'no' }}, }; $_->() for values %$subs; cmpthese( $ARGV[0] => $subs );

And to finally (not) answer your question: The ?: operator is much "lighter" than if-else, it is one expression without scope, whereas the if-block can have it's own lexicals. If you wanted to have the functionaltiy of if-else in a ?: construct, you'd have to use do BLOCK, which already slows down the whole thing...

UPDATE: Aristotle is of course right about the "ternary" operator.

--
http://fruiture.de

Replies are listed 'Best First'.
Re^2: if-else vs. tertiary effieciency?
by Aristotle (Chancellor) on Sep 14, 2002 at 15:18 UTC
    btw. it's a 'trinary' operator.
    If you correct someone's vocabulary, please do it right. :-) The term is ternary.

    Makeshifts last the longest.

      Actually, either "Ternary" or "Trinary" is acceptable.

      --MrNobo1024
      s]]HrLfbfe|EbBibmv]e|s}w}ciZx^RYhL}e^print

        Huh - ya learn something new every day. Thanks.

        Makeshifts last the longest.

      I'm sinking into the ground... thanks (must have come from the german "trinär")

      --
      http://fruiture.de
        this actually is called "ternär" in the german language :) now stop this "erbsenzählerei" ;)
Re: Re: if-else vs. tertiary effieciency?
by blaze (Friar) on Sep 14, 2002 at 23:15 UTC
    First of all, i'd write:print $var ? 'yes' : 'no' wouldnt this print yes or no depending on $var...it looks like the Anonymous Monk wants to print yes or no depending on whether $var has a value or not
    $var ? print 'yes' : print 'no'
    seems right to me, am i missing something?

    Update: I just started learning how ?: works, so i just wanted to know why
    print $var ? 'yes' : 'no'
    will also work, i guess im not understanding how it evaluates $var...the way im reading ?: is if 1st arg (print $var) then second (yes) else third(no) so how does it know that you actually want to print yes or no? with the way im looking at it, it is
    if(print $var){ 'yes' }else{ 'no' }
    anyone care to explain to me, id appreciate it greatly
      Think of it as:
      print ( $boolean ? $true_value : $false_value );
      or in your terms:
      print ( if($var}{ 'yes' }else{ 'no' } );
      I like this less because it is pseudo-code that looks
      much like code. So it seems to imply many things that
      aren't true. The conditional operator is only an
      operator not a statement.
        so the print isnt part of the ?: in fruiture's example...if thats the case then it makes beautiful sense and i cant believe i didnt see it before, if thats not the case im a little more lost than i was to begin with :)

        ill be sure to ++ you when i get my next set of votes ;)