Function unrolling (was Re: Speed: bytecode vz C API calls)

Jacek Generowicz jacek.generowicz at
Wed Dec 10 17:45:27 CET 2003

aahz at (Aahz) writes:

> That's why I changed the Subject: to "Function unrolling"; you're going
> to need to change your code to avoid function calls in inner loops.  One
> technique you might try is to create a generalized inner loop function:
> def innerloop(iterable, func, cache):
>     l = []
>     for args in iterable:
>         try:
>             l.append(cache[args])
>         except KeyError:
>             l.append(cache.setdefault(args, func(args))
>     return l

OK, I see what you mean.

I agree, I can do this, in some places, and toy tests suggest that
getting rid of the function call could give me a factor of 3
improvement. I'll have to change the code in many places, but that's
simpler than going to C.

(However, we disagree on the specific formula you give above: I think
that map is faster: see other post in the thread. [A quick test
suggests that a map is faster than the loop by a factor of 2.])

> Yes, it's a performance hack, but I bet it's a lot clearer than C++
> code.

No doubt about that :-)


More information about the Python-list mailing list