[C++-sig] Re: Newbie : fastest way of sending floats

Raoul Gough RaoulGough at yahoo.co.uk
Sun Nov 23 12:01:38 CET 2003


Philip Austin <paustin at eos.ubc.ca> writes:

> Benjamin Golinvaux writes:
>  > 
>  > I am not looking for the cleanest code, but for the FASTEST way
>  > to pass the array of floats. I am willing to use the raw C api if
>  > needed.
>
> The fastest way to pass an array of floats is to use Numerical
> Python (http://www.pfdubois.com/numpy).  Boost supports conversions
> between numpy (and its eventual successor, numarray) and python.
> Here's an example of a wrapper for a spline interpolator, which also
> makes use of a boost/numpy support library available at
> http://www.eos.ubc.ca/research/clouds/num_util.html
>
> py::numeric::array splinewrap(py::numeric::array x, py::numeric::array y, 
> 			      double yp1, double ypn)
> {
>   nbpl::check_rank(x,1);
>   nbpl::check_type(x,PyArray_DOUBLE);
>   int xsize=nbpl::size(x);
>   nbpl::check_rank(y,1);
>   nbpl::check_type(y,PyArray_DOUBLE);
>   int ysize=nbpl::size(y);
>   assert(xsize==ysize);
>   double* xptr=(double*) nbpl::data(x);
>   double* yptr=(double*) nbpl::data(y);
>   py::numeric::array y2=nbpl::makeNum(ysize, PyArray_DOUBLE);
>   double* y2ptr=(double*) nbpl::data(y2);
>   spline(xptr,yptr,ysize,yp1,ypn,y2ptr);
>   return y2;
> }

Another alternative would be to store the data in a C++ vector and
access it from Python via the indexing suite (available in the
CVS). From the looks of the above code, this would give you a more
convenient interface, but there is an important performance trade-off
to consider: every time you access the vector from Python, you go
through the Boost.Python function dispatch code, which adds a
significant overhead if the operation itself is very simple. It's not
a problem when processing the entire vector in C++ via a single call
from Python, but probably *is* a problem if you spend a lot of time
handling or inserting single values from Python code.

Here are some comparative performance results from my testing of the
(indexing_v2) indexing suite:

Operation on vector (sec)          *Python list time (inverted)
=========================
Fill: 1.95659923258                5.1706970443
Search: 0.0113099569917            0.0755311358728 (13.23957317)
Reorder: 27.9347512044             7.83041287513
Sort: 0.153025035305               0.0575254278229 (17.3836169125)

The test fills a vector<int>, does some searches on it, scrambles the
contents and then sorts the contents. The column at right shows the
runtime relative to the same operations in pure Python using a Python
list. As you can see, filling and reordering the vector (which handles
values one at a time) from Python are 5 or 8 times slower, whereas the
search and sort functions (which handle the whole vector in C++ via a
single Python call) are 13 or 17 times faster.

-- 
Raoul Gough.
export LESS='-X'





More information about the Cplusplus-sig mailing list