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

I

iviskic

Hi,

I'm doing an analysis of program code performance when written in python
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
references? How EXACTLY does it work? I've read the documentation, but it
is written on a cooking-book principle, so it does not say clearly how it
works, just that it does work.

Please let me know if you now how it's done, or (preferably) point me
where to look for the answer.
Thank you in advance.

Ines
 
A

Alex Martelli

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top