[Numpy-discussion] trying to speed up the following....
Tue Mar 24 18:39:49 CDT 2009
On Tue, Mar 24, 2009 at 18:29, Brennan Williams
> I have an array (porvatt.yarray) of ni*nj*nk values.
> I want to create two further arrays.
> activeatt.yarray is of size ni*nj*nk and is a pointer array to an active
> cell number. If a cell is inactive then its activeatt.yarray value will be 0
> ijkatt.yarray is of size nactive, the number of active cells (which I
> already know). ijkatt.yarray holds the ijk cell number for each active cell.
> My code looks something like...
> for ijk in range(0,ni*nj*nk):
> if porvatt.yarray[ijk]>0:
> I may often have a million+ cells.
> So the code above is slow.
> How can I speed it up?
mask = (porvatt.yarray.flat > 0)
ijkatt.yarray = np.nonzero(mask)
# This is not what your code does, but what I think you want.
# Where porvatt.yarray is inactive, activeatt.yarray is -1.
# 0 might be an active cell.
activeatt.yarray = np.empty(ncells, dtype=int)
activeatt.yarray[mask] = ijkatt.yarray
"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