how does CPython work?

Peter Otten __peter__ at web.de
Wed May 26 15:16:24 EDT 2004


beliavsky at aol.com 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





More information about the Python-list mailing list