[Numpy-discussion] Quick Question about Optimization

James Snyder jbsnyder@gmail....
Tue May 20 20:01:37 CDT 2008

> Well, if you do f = a[n, :], you would get a view, another object that
> shares the data in memory with a but is a separate object.

OK, so it is a new object, with the properties of the slice it
references, but if I write anything to it, it will consistently go
back to the same spot in the original array.

In general, if I work on that, and don't do something that allocates a
new set of memory locations for that, it will reference the same
memory location.

If I do:
a = np.zeros((20,30))
b = a[2,:]

b += 1 # will add 1 to the original slice

b.resize will fail...

b = np.zeros((1,30)) # allocates new memory and disconnects the view

The appropriate way to zero out the original memory locations would be
to do something like b *= 0?

Is there any way to force a writeback to the original view so long as
the dimensions of what is being assigned to b is the same as the
original? Or, is there a way to, say, enable a warking if I'm dropping
a view?

> Sorry I didn't get back to you earlier on this -- I was a bit busy
> yesterday.  It looks like weave.blitz isn't working on your second
> line because you're not explicitly putting slices in some of the
> dimensions,  In numpy v[0:2] works for 1,2,3,4,.... dimensions, but
> for a 2d array in blitz you have to use v[0:2,:], 3d v[0:2,:,:].  It's
> a bit more picky.   I think that's the problem with your second line
> -- try replacing v[:] with v[0,:] and theta[1-curidx] with
> theta[1-curidx, :]. (I may have missed some others.)

OK, that seems to do it.  I still actually get better performance
(subsequent runs after compilation) with the straight numpy code.
Strangely, I'm also getting that the flip/flop method is running a bit
slower than having the separate prev_ variables.  aff_input is rather
large (~2000x14000), but the state vectors are only 14000 (or that x2
w/ flipflopping for some), each.

Is there slowdown maybe because it is doing those 3 lines of blitz
operations then doing a bunch of python numpy?

Either way, It seems like I've got pretty good performance as well as
a handle on using weave.blitz in the future.


James Snyder
Biomedical Engineering
Northwestern University
PGP: http://fanplastic.org/key.txt

More information about the Numpy-discussion mailing list