C-extension 2 times slower than exe

Carl Banks pavlovevidence at gmail.com
Tue Jun 23 19:57:20 EDT 2009

On Jun 23, 7:20 am, Rolf Wester <rolf.wes... at ilt.fraunhofer.de> wrote:
> Philip Semanchuk wrote:
> > On Jun 23, 2009, at 9:51 AM, Rolf Wester wrote:
> >> Hi,
> >> I have a C++ program that I would like to steer using Python. I made the
> >> wrapper using swig and linked the code (without the main function) into
> >> a shared object. My Python script loads the extension and calls a
> >> function of the C-extension, the rest runs entirely within the
> >> C-extension. For comparison I compiled the code including the main
> >> function with the same compilation options and linked all into an exe.
> >> The main function of the exe calls the same function as my Python script
> >> does. Surprisingly the code in the Python C-extension runs twice as long
> >> as the same code in the exe. Does anyone know what could be the reason
> >> for this behaviour?
> > If the runtime of the C/C++ code is short, the time spent initializing
> > the Python interpreter might have a big impact on the runtime of the
> > Python version.
> The runtime is about 2.5 sec and 5.0 sec respectively. I not only use
> the time command to measure the time consumed but I also measure the
> time within the C-code using clock() and get similar result. So the
> Python startup time is not the reason for the runtime difference I
> found. Thank you anyway.

We can't really help you much unless you give us more details about
what you did (how did you built it, how did you time it, and how are
you calling the C extension from Python).  All we can do is throw out
possible ideas, and I will toss out a few, but if that's not it you'll
have to post details.

Are you building the extension with the same optimization flags as the

Are you calling the C code repeatedly from inside a Python loop?

More information about the Python-list mailing list