Basic optimization of python.

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Apr 9 09:46:52 EDT 2008


 ShenLei:

> 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.

Removing dots creating a temporary variable speeds up the program, but
doing it is useful only in special critical spots, like inside certain
large loops, etc.
A similar optimization is to use a local instead of a global:

from foo import bar
def baz(bar=bar):
    for i in xrange(100000):
        bar(...)

Note that with psyco you often don't need such things.

Bye,
bearophile



More information about the Python-list mailing list