Steve Howell showell30 at
Fri Jun 1 19:16:09 CEST 2007

--- Paul Boddie <paul at> wrote:

> On 1 Jun, 12:55, Steve Howell <showel... at>
> 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.
> On the basis of you believing that x is one of the
> built-in numeric
> types, yes, but how does the compiler know that?

Disclaimer up front--premature optimization is the
root of all evil, etc.

But to answer the question...

The compiler doesn't know the types up front, but if
you wanted to do this kind of optimization (and you
believed that 95% of x*x cases would benefit from it,
and you're willing to sacrifice performance for the 5%
of folks that overload multiply), then the compiler
could generate bytecode that set the stage for later
conditional caching of the first execution of x*x.

You'd then need the execution of the bytecodes at
runtime (ceval.c or something called by it) to work in
such a way that they only cache the result when side
effects are not an issue.  At runtime you can reliably
detect whether something is still a virgin builtin,

To my disclaimer, you would only undertake such an
optimization if multiplication were really, really
expensive (which I don't think is even true for floats
today), and even then you'd proceed cautiously.

Get your own web address.  
Have a HUGE year through Yahoo! Small Business.

More information about the Python-list mailing list