Basic optimization of python.

Diez B. Roggisch deets at
Wed Apr 9 13:54:01 CEST 2008

甜瓜 wrote:

> Howdy,
>     I wonder whether python compiler does basic optimizations to .py.
> Eg:
> t = self.a.b
> t.c = ...
> t.d = ...
> .vs.
> self.a.b.c = ...
> self.a.b.d = ...
> which one is more effective? Since each dot invokes a hash table lookup,
> it may be time consuming. If the compiler can do expression folding, then
> no manual folding is needed.

It can't do that because of no guarantee can be made that self.a has no
side-effects, and consequently doesn't do it.

> Again, how about contant calculation?
> Eg:
> a = 1 + 2
> .vs.
> a = 3
> which one is more effective? Does the compiler calculate the result at
> compile time? How about constant spreading?

Algebraic optimizations aren't done AFAIK - and it's debatable if the
mu-secs saved by that would be worth the effort, as if *they* matter, you
obviously are in number-crunching mode and should resort to libs such as
Numpy to do your work.


More information about the Python-list mailing list