performance question when using embedding/extending

Gordon McMillan gmcm at hypernet.com
Mon Jan 31 14:30:39 EST 2000


Eibl Christian writes:
> 
> i need to call a python function from my c++ app and this python function in
> turn
> accesses an instanciated c++ object (i pass the pointer to the object as a
> parameter).
[snip]
> i "quantified" the application an see that almost all the cpu-time is used
> in "PyEval_CallObject".
> the application i'm building needs at least 10-times the performance it has
> now...
> 
> my question:
>  - is this just the (slow) way it is ?

Python functions have a relatively high overhead.

>  - is there a faster way to use embedding/extending (details see below) ?

Getting rid of SWIG and doing a bare-metal C layer yourself 
should (at a guess) meet your performance goals. That 
probably means associating a your C++ object with an 
extension type (see the extending docs) so the Python -> 
C++ stuff needs no shadow class or string / pointer conversion.

You could look at CXX (from the LLNL download - link on the 
Python home page) which is a full-featured Python / C++ API. 
I have a lighter-weight one (scxx - see my starship pages); for 
an example of a truly perverse way of making an extension 
type and a C++ class almost the same thing, see how scxx is 
used in the Python bindings of MetaKit (you'll find that on 
SourceForge).


- Gordon




More information about the Python-list mailing list