[SciPy-user] Performance question.

Duncan Child duncan at enthought.com
Mon Mar 7 11:18:41 CST 2005

Thanks for the suggestion Robert. If we ignore the elegance requirement 
then handling scalars as a special case in pure Python is an adequate 

However, I am hoping that Numeric3 will allow us to write more elegant 
functions that perform well on both scalars and arrays.



Robert Kern wrote:

> Duncan Child wrote:
>> I am working on developing algorithms that are usually called with 
>> parameters that are Numeric arrays.  We have the usual challenge 
>> though of trying to craft code that will gracefully accept both 
>> floats or arrays. Because of the often discussed problem with the 
>> handling of zero length arrays we expend some effort to ensure that 
>> we don't make calls on floats that only work on arrays and have ended 
>> up with a bunch of code like safe_len() that can be called with either.
>> Today we have a related problem but now it is with performance (see 
>> code below). Numeric is faster than NumArray operating on smaller 
>> arrays but it is still relatively slow handling regular floats. We 
>> could add to the safe_ suite of functions the fast_ series but this 
>> still entails a significant performance hit and is not exactly elegant.
> A potential solution to the performance problem, though not the 
> elegance problem, might be Pyrex.
> An untested sketch:
> cdef extern from "Numeric/arrayobject.h":
>     bool PyArray_Check(object x)
> cdef extern from "math.h":
>     double sqrt(double x)
> import Numeric
> def fast_sqrt(object x):
>     if PyArray_Check(x):
>         return Numeric.sqrt(x)
>     else:
>         return sqrt(x)

More information about the SciPy-user mailing list