Why is function call - recursive especially - in python so slow?

Alex Martelli aleaxit at yahoo.com
Mon Oct 18 22:37:42 CEST 2004


<iviskic at uci.edu> wrote:
   ...
> as oppose to other programming languages, and can't seem to figure out why
> is the call of functions so slow?
> Is a context being created and stored on stog each time, or does it uses

A frame object is created for each call.

> references? How EXACTLY does it work? I've read the documentation, but it
   ...
> Please let me know if you now how it's done, or (preferably) point me
> where to look for the answer.

The source code is the only ultimate answer to such implementation
questions.  Download Python's sources and see how frame objects are
created, populated, and stacked on call, and popped and destroyed on
return (or on exit by exception propagation) from a function.  ceval.c
has the huge switch on bytecodes that shows you what happens at each
bytecode -- to see what bytecode comes from a code fragment, use the dis
module (dis.dis of a small codeobject or function is instructive).  Once
you see the calls which a certain bytecode causes ceval.c to make, you
can track the details over other source files.

Or if you prefer to see the implementations done in Java or C# or
O'Caml, all of Jython, IronPython and Vyper are open-source, too, of
course.  You can see an incomplete implementation in Python itself in
pypy, another one in (I'm not sure, I guess perl?) in PyParrot.  Of
course, each implementation can do what it wishes, as long as it
respects Python language semantics.


Alex



More information about the Python-list mailing list