how does CPython work?

Discussion in 'Python' started by beliavsky@aol.com, May 26, 2004.

  1. Guest

    The book "Learning Python" says that a "Python Virtual Machine"
    interprets "byte code" to run a Python program. Is there a book or
    site that describes in more detail what Python is doing behind the
    scenes? One reason for my question is that I want to better understand
    what kinds of Python programs run relatively fast or slow, rather than
    relying solely on trial and error.
     
    , May 26, 2004
    #1
    1. Advertising

  2. Tor Iver Wilhelmsen, May 26, 2004
    #2
    1. Advertising

  3. Peter Otten Guest

    wrote:

    > The book "Learning Python" says that a "Python Virtual Machine"
    > interprets "byte code" to run a Python program. Is there a book or
    > site that describes in more detail what Python is doing behind the
    > scenes? One reason for my question is that I want to better understand
    > what kinds of Python programs run relatively fast or slow, rather than
    > relying solely on trial and error.


    I'm not really competent here, but I'll try to make up for it by bold
    guesses.

    Use the dis module to study the byte code generated by the Python compiler.
    Look into Python's C source, particularly ceval.c, to learn how long each
    byte code takes to execute. This should be no problem once you know the
    speed of the corresponding machine instructions which are generated by a
    compiler - you've got a streak of luck - written in C. How fast would the
    machine be? No problem, once you know the underlying microcode and the
    processor's general layout - just follow those diligent electrons on their
    way through the silicon. Unfortunately you are way beyond the realm of open
    source here. But who cares - let's just write a script that does what
    you're most interested in in the language you are planning to use anyway
    and profile that.
    Profiling is, by the way, the means of choice for trial and error speedup
    within the bounds of reason. In particular it saves you from optimizing
    parts of the code that are executed only a few times during the run of the
    application.

    :)

    If you are just interested in Python's inner workings and know some C - the
    source code is well organized, so you won't get lost. When you are able to
    make reliable performance predictions by looking at the code, you are
    probably better than many Python contributers and should become a core
    developer...

    Peter
     
    Peter Otten, May 26, 2004
    #3
  4. wrote:
    > The book "Learning Python" says that a "Python Virtual Machine"
    > interprets "byte code" to run a Python program. Is there a book or
    > site that describes in more detail what Python is doing behind the
    > scenes? One reason for my question is that I want to better understand
    > what kinds of Python programs run relatively fast or slow, rather than
    > relying solely on trial and error.


    beside what has already been said concerning code execution you
    should get familiar with python's object model to understand
    what a method call implies, for example. Symbols are looked up
    in dictionaries (all method objects are usually accessed through
    an instance's '__dict__' dictionary for example), so being careful with
    the related issues will help you profile and optimize your code, too.

    Regards,
    Stefan
     
    Stefan Seefeld, May 27, 2004
    #4
    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. York
    Replies:
    1
    Views:
    386
    Chris Liechti
    Sep 10, 2003
  2. Boris Boutillier

    Compiling a CPython library on Mac OSX

    Boris Boutillier, Dec 12, 2003, in forum: Python
    Replies:
    3
    Views:
    396
  3. David McNab
    Replies:
    0
    Views:
    520
    David McNab
    Apr 9, 2004
  4. Peter Otten
    Replies:
    0
    Views:
    76
    Peter Otten
    Feb 17, 2014
  5. Steven D'Aprano
    Replies:
    2
    Views:
    91
    Terry Reedy
    Feb 17, 2014
Loading...

Share This Page