Pyrex and weave.accelerate

Patrick Miller patmiller at llnl.gov
Mon Apr 8 16:56:28 EDT 2002


eric wrote:
> I'm also alluding to creating C/C++ versions of Python functions
> that have a standard C calling convention (instead of the self, args)
> stuff that has to be parsed on function calls.  If a pure C version

The other issue is the "callback" issue.  A common pattern in
scipy is to pass in a function as an argument

e.g.
>>> integrate(0.0,1.0, sin)

Now, there is a HUGE speed gain to be had if

def foo(x):
   return x*x - 2*x + 1

could be called without any of the Python overhead.  Right now,
the internal bit of the integration function has to look like

   PyObject* result = PyObject_CallFunction(f,"d",x);
   double fx = PyFloat_AsDouble(result);

This in a really tight loop....  We're looking at
a way that one could query an object for its
underlying C function and call that

   PyObject* functionAddress = PyObject_CallMethod(f,"asCFunction","");
   long functionAddressAsLong = PyInt_AsLong(functionAddress);
   double (*function)(double) = reinterpret_cast<....>(functionAddressAsLong);
   for(...) {
       double fx = function(x);
   }

Which is noticably faster.

-- 
Patrick Miller | (925) 423-0309 | patmiller at llnl.gov

Knowing is not enough; we must apply. Willing is not enough; we must do.
-- Johann Wolfgang von Goethe (1749-1832)





More information about the Python-list mailing list