[SciPy-user] Performance question.

Robert Kern rkern at ucsd.edu
Wed Mar 2 18:33:57 CST 2005


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)

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the SciPy-user mailing list