[Numpy-discussion] Re: Speed up function on cross product of two sets?

Robert Kern robert.kern at gmail.com
Mon Apr 3 10:19:02 CDT 2006


Zachary Pincus wrote:
>> If I were going to make a list it would look something like:
>>
>> 0. Think about your algorithm.
>> 1. Vectorize your inner loop.
>> 2. Eliminate temporaries
>> 3. Ask for help
>> 4. Recode in C.
>> 5 Accept that your code will never be fast.
>>
>> Step zero should probably be repeated after every other step ;)
> 
> Thanks for this list -- it's a good one.
> 
> Since we're discussing this, could I ask about the best way to 
> eliminate temporaries? If you're using ufuncs, is there some way to 
> make them work in-place? Or is the lowest-hanging fruit (temporary-
> wise) typically elsewhere?

Many binary ufuncs take an optional third argument which is an array which the
ufunc should put the result in.

In [2]: x = arange(10)

In [3]: y = arange(10)

In [4]: id(x)
Out[4]: 91297984

In [5]: add(x, y, x)
Out[5]: array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

In [6]: id(Out[5])
Out[6]: 91297984

In [7]: x
Out[7]: array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

-- 
Robert Kern
robert.kern at gmail.com

"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