Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^5: Why is the execution order of subexpressions undefined? (sane?)

by BrowserUk (Patriarch)
on Apr 12, 2005 at 06:08 UTC ( [id://446863]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Why is the execution order of subexpressions undefined? (sane?)
in thread Why is the execution order of subexpressions undefined?

Sub expressions are evaluated before the expressions they are a part of. Order of evaluation within a subexpression is in precedence order with ties broken left to right.

In the expression you gave, both ( $y + 1 ) are subexpressions and would be evaluated before the overall expression, left to right. The fact that they are the same subexpression gives rise to the possible optimisation.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco.
Rule 1 has a caveat! -- Who broke the cabal?
  • Comment on Re^5: Why is the execution order of subexpressions undefined? (sane?)
  • Download Code

Replies are listed 'Best First'.
Re^6: Why is the execution order of subexpressions undefined? (sane?)
by tye (Sage) on Apr 12, 2005 at 06:24 UTC

    So does that mean $z is calculated before $y+1 or not? In what order does $w get calculated?

    $w= $z; $x= ( $a + $w ) * ( $y + 1 ) + ( $a + $z ) / ( $y + 1 );

    The optimizer notices that $w and $z must be equal and so we need only add $a to their shared value once. In what order does this single operation happen relative to the rest of the calculations? If I change the first assignment, surely the order of operations for the second assignment doesn't change. What is the obvious answer to this?

    - tye        

      The optimizer notices that $w and $z must be equal...

      A C or C++ compiler might be able to make that optimisation, but Perl--No way!

      What if $w is tied? What if my Bit2 $w; and my Bigint $z;?

      Bogus question. Next?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco.
      Rule 1 has a caveat! -- Who broke the cabal?

        Note that tye mentioned perl6, which will allow you to mention the types of your variables, so that the compiler /can/ tell.


        Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://446863]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-23 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found