[Numpy-discussion] Array of Callables

Robert Kern robert.kern@gmail....
Tue Mar 20 12:10:43 CDT 2007


Shane Holloway wrote:
> To the vector-processing masters of numpy!
> 
> I'm wanting to optimize calling a list (or array) of callable  
> objects.  Consider the following:
> 
> vCallables = numpy.array([<python callables: fns, methods, lambdas,  
> classes, builtin functions>])
> vParam1 = numpy.array([<parameters for vCallables>])
> vParam2 = numpy.array([<parameters for vCallables>])
> vParam3 = numpy.array([<parameters for vCallables>])
> vResults = numpy.array([None for e in vCallables])
> 
> Is it possible to have numpy call them in a way something like::
> 
> 	for i in xrange(vCallables.shape[0]):
> 		vResults[i] = vCallables[i] ( vParam1[i], vParam2[i], vParam3[i]  )
> 
> 
> With the core of that loop implemented in C?

There's nothing sitting around, no. It's easy enough to write with Pyrex (and
easier without bothering with numpy); however, unless if all of your functions
are builtin, you won't see much, if any, speed gains. Python's function call
overhead will dominate the savings of a C for loop over a Python for loop.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list