how does CPython work?

B

beliavsky

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.
 
P

Peter Otten

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
 
S

Stefan Seefeld

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
 

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
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top