speedup numpy ufuncs

Robert Kern kern at caltech.edu
Wed Jun 21 19:47:12 EDT 2000


Pete Shinners wrote:
> 
> i've been doing some simplish image processing
> stuff with numpy and a thought occured to me about
> a possible speedup.
> 
> if i have 4 or 5 arrays i want to add together,
> the fastest way i've found to do this is by
> calling the add ufunc many times, specifying an
> output argument.
> (this gets rid of extra temporary arrays)
> 
> array1 = array2 = array3 = array4 = arange(100)
> add(array1, array2, array1)
> add(array1, array3, array1)
> add(array1, array4, array1)
> 
> i was wondering if there was a way to add these all
> in one pass?
> addmany(array1, array2, array3, array4, array1)

This is where you get to the really cool part of ufuncs.  ufuncs have
methods.  You want the "reduce" method.

new_array = add.reduce([array1, array2, array3, array4])

You make a single new array.  I don't believe there is a way to put the
results into an old array.

[snip]

--
Robert Kern
kern at caltech.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 Python-list mailing list