perl5ever has asked for the wisdom of the Perl Monks concerning the following question:

Is this assignment:
my $check = ($y eq $z);
any less efficient than this?
my $check = $y eq $z;
I'd like to know if perl is able to avoid creating a one-element list which it will just immediately throw away after assigning $check to its first element.

Thanks...

Replies are listed 'Best First'.
Re: Are these grouping parens or list creation parens?
by ikegami (Patriarch) on Oct 01, 2009 at 20:44 UTC

    Are these grouping parens or list creation parens?

    There's no such thing as list-creating parens. Lists are created by commas and where they are needed. Parens override precedence, modify certain operators (e.g. = vs ()=, x vs ()x, eof vs eof(), etc), and indicate a lack of operands when empty.

    I'd like to know if perl is able to avoid creating a one-element list

    No.

    $x = ("a","b","c");
    compiles to
    pushmark const "c" list $x sassign
    and not
    const "c" $x sassign

    That case of a list in scalar or void context could be optimised (if it's known at compile-time), but the savings would be negligible since pushmark and list are extremely cheap operations O(1) operations.

    Is this assignment: my $check = ($y eq $z); any less efficient than this? my $check = $y eq $z;

    No. Their compilation produces identical outputs.

      There's no such thing as list-creating parens. Lists are created by commas and where they are needed. Parens override precedence, modify certain operators (e.g. = vs ()=, x vs ()x, eof vs eof(), etc), and indicate a lack of operands when empty.
      That's very enlightening - thanks!
Re: Are these grouping parens or list creation parens?
by Corion (Patriarch) on Oct 01, 2009 at 19:42 UTC
    >perl -MO=Deparse -e "my $check = ($y eq $z);my $check = $y eq $z;" my $check = $y eq $z; my $check = $y eq $z; -e syntax OK

    Also, if Perl really were creating a one-element list, that one-element list would be true even if it only contained one undef element:

    > perl -wle "my @hypothetical = (1 == 0); my $check = @hypothetical; p +rint $check" 1

    So, Perl knows an expression when it sees one, and the expression is a simple boolean result, and nothing complicated.

    Mostly though, if you are worrying about such minuscule optimizations, you should either be using C or spend the time about rethinking your approach to the problem, possibly either reworking your algorithms or reworking your data structures. In both, you'll find far more possibilities to speed up your program than in musings about how Perl evaluates boolean expressions.

Re: Are these grouping parens or list creation parens?
by BrowserUk (Patriarch) on Oct 01, 2009 at 19:47 UTC

    Even I, who takes pessimisation avoidance seriously, is (am?) having considerable trouble taking this question seriously. (Geez that is clumsy wording.)

    More so because you purport to be worried enough to ask the question, but not enough to take the simplest steps to discover the answer for yourself. Vis:

    C:\test>perl -MO=Deparse -e" my $check = ($y eq $z); " my $check = $y eq $z; -e syntax OK C:\test>perl -MO=Deparse -e"my $check = $y eq $z;" my $check = $y eq $z; -e syntax OK

    Why bother responding. Simple. Cos it took less tham 2 minutes to do so--and the adverts last 3.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.