Simple question about how the optimizer works

James J. Besemer jb at cascade-sys.com
Fri May 10 04:04:36 EDT 2002


jb wrote:

> And what about pure calculations? Let for example a,b and c be long
> integers. Then what about
>
> x1=a*b*c+1-3
> y1=sqrt(a*b*c+7)
>
> or more simply
>
> res= a / b
> rest = a % b
>
> Can such calculations be optimized?

Again, the compiler would need to be able to determine the types of the variables
a,b,c,d and make sure it was safe to remove the common sub-expressions..  E.g, in
the context where it is known that

    a = 3
    b = a+1
    c = 99

it would be a valid optimization.

This is a bit tricky in a dynamic language like Python.  E.g., in "a*b" a could
be a list or arbitrary objects that peversely define "*" or "+" to be operations
with side-effects.  Even if you had a blanket rule to not optimize where
user-defined operators are involved, it is non-trivial to determine at compile
time what the type of the variables will be.

Regards

--jb

--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax
mailto:jb at cascade-sys.com







More information about the Python-list mailing list