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

Discussion in 'Python' started by iviskic@uci.edu, Oct 18, 2004.

  1. Guest

    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
    , Oct 18, 2004
    #1
    1. Advertising

  2. <> 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
    Alex Martelli, Oct 18, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    3
    Views:
    375
    Virgil Green
    Jul 11, 2005
  2. Terry Reedy
    Replies:
    0
    Views:
    501
    Terry Reedy
    Oct 19, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  4. n00m
    Replies:
    12
    Views:
    1,102
  5. Alok
    Replies:
    3
    Views:
    239
Loading...

Share This Page