Embedding performance.

k33rni at gmail.com k33rni at gmail.com
Mon Jul 4 11:44:49 CEST 2005


amit wrote:
>  Is there any kind of performance differences to the different ways of
> embedding python?
>       PyEval_EvalCode()
>       PyRun_SimpleFile()
>       PyObject_CallObject()
>

AFAIK, these are only different wrappers, while you're generally
getting at the same functionality. I assume you're somewhat familiar
with the Python API. What you're saving using the third one is parsing
and compiling time, since the other two functions do it every time
they're called, I believe. With PyObject_CallObject() you already have
the PyObject, so it just goes straight to the interpreter core, and
does not do any parsing and compiling. That said, you have to get the
object from somewhere first, possibly using the other two :), which may
not apply if you're calling something embedded (that is, written in C).


A quick look in the Python sources brings up the following:
PyRun_SimpleFile: adds __main__. sets __file__. looks for .pyc or .pyo
files, loads the code object from them, or parses text, and finally
calls PyEval_EvalCode with the code object. So there, you have the
overhead.
PyObject_CallObject: goes through some redirection to PyObject_Call,
which calls obj->ob_type->tp_call directly.
PyEval_EvalCode: checks for globals, checks for arguments, creates a
frame with all the necessary stuff, checks for generators, calls
PyEval_EvalFrame() which does all the dirty running of python code.

So there, the least overhead seems to be in CallObject. Don't treat
this as an authoritative answer, though.:)




More information about the Python-list mailing list