Re: subexpressions - Python

This is a discussion on Re: subexpressions - Python ; --- Tijs <tijs_news@bluescraper.nl> wrote: > Steve Howell wrote: > > FWIW there's the possibility that even without a > > subexpression syntax, some Python implementations > > would detect the duplication of x*x and optimize > that > > for ...

+ Reply to Thread
Results 1 to 2 of 2

Re: subexpressions

  1. Default Re: subexpressions


    --- Tijs <tijs_news@bluescraper.nl> wrote:

    > Steve Howell wrote:
    > > FWIW there's the possibility that even without a
    > > subexpression syntax, some Python implementations
    > > would detect the duplication of x*x and optimize

    > that
    > > for you. It would have to know that x*x had no

    > side
    > > effects, which I think is a safe assumption even

    > in a
    > > dynamic language like Python.

    >
    > No, x may be an object that has the __mul__ special
    > method, and it may have
    > side effects.
    >


    Ok, I stand corrected.

    Duplicate subexpressions are pretty easy to avoid in
    Python, so though an optimization would not be
    impossible here (checking for immutability of
    builtins, etc., which still assumes the idea that
    multiplication is more expensive than checking for
    immutability even for the common builtin case), it
    would not be worthwhile.

    Shortly after I posted, there was an elegant solution
    to avoiding having to repeat x*x in the lambda, so the
    point's kind of moot now.




    ____________________________________________________________________________________
    Shape Yahoo! in your own image. Join our Network Research Panel today! http://surveylink.yahoo.com/gmrs/yah...invite.asp?a=7



  2. Default Re: subexpressions

    > Ok, I stand corrected.
    >
    > Duplicate subexpressions are pretty easy to avoid in
    > Python, so though an optimization would not be
    > impossible here (checking for immutability of
    > builtins, etc., which still assumes the idea that
    > multiplication is more expensive than checking for
    > immutability even for the common builtin case), it
    > would not be worthwhile.
    >
    > Shortly after I posted, there was an elegant solution
    > to avoiding having to repeat x*x in the lambda, so the
    > point's kind of moot now.


    The elegance of that solution very much depends on the cost of the duplicate
    operation vs. the additional function call.

    And for the usecase at hand, that's exactly the point not to do it:

    droggisch@ganesha:/tmp$ python -m timeit '(lambda x: lambda y: y+y)(10 *
    10)'
    1000000 loops, best of 3: 1.04 usec per loop
    droggisch@ganesha:/tmp$ python -m timeit 'lambda: 10 * 10 + 10 * 10'
    1000000 loops, best of 3: 0.336 usec per loop


    Diez

+ Reply to Thread

Similar Threads

  1. subexpressions
    By Application Development in forum Python
    Replies: 20
    Last Post: 06-03-2007, 07:35 PM
  2. Re: subexpressions
    By Application Development in forum Python
    Replies: 2
    Last Post: 06-02-2007, 09:39 AM
  3. Re: subexpressions
    By Application Development in forum Python
    Replies: 0
    Last Post: 06-01-2007, 11:59 AM
  4. Re: subexpressions
    By Application Development in forum Python
    Replies: 2
    Last Post: 06-01-2007, 07:08 AM
  5. Re: subexpressions
    By Application Development in forum Python
    Replies: 0
    Last Post: 06-01-2007, 06:03 AM