# [Numpy-discussion] Quick Question about Optimization

James Snyder jbsnyder@gmail....
Mon May 19 13:08:49 CDT 2008

```Hi -

First off, I know that optimization is evil, and I should make sure
that everything works as expected prior to bothering with squeezing
out extra performance, but the situation is that this particular block
of code works, but it is about half as fast with numpy as in matlab,
and I'm wondering if there's a better approach than what I'm doing.

I have a chunk of code, below, that generally iterates over 2000
iterations, and the vectors that are being worked on at a given step
generally have ~14000 elements in them.

In matlab, doing pretty much exactly the same thing takes about 6-7
seconds, always around 13-14 with numpy on the same machine.  I've
gotten this on Linux & Mac OS X.

self.aff_input has the bulk of the data in it (2000x14000 array), and
the various steps are for computing the state of some afferent neurons
(if there's any interest, here's a paper that includes the model:
Brandman, R. and Nelson ME (2002) A simple model of long-term spike
train regularization. Neural Computation 14, 1575-1597.)

I've imported numpy as np.

Is there anything in practice here that could be done to speed this
up?  I'm looking more for general numpy usage tips, that I can use
while writing further code and not so things that would be obscure or
difficult to maintain in the future.

Also, the results of this are a binary array, I'm wondering if there's
anything more compact for expressing than using 8 bits to represent
each single bit.  I've poked around, but I haven't come up with any
clean and unhackish ideas :-)

Thanks!

I can provide the rest of the code if needed, but it's basically just
filling some vectors with random and empty data and initializing a few
things.

for n in range(0,time_milliseconds):
self.u  =  self.expfac_m  *  self.prev_u +
(1-self.expfac_m) * self.aff_input[n,:]
self.v = self.u + self.sigma *
np.random.standard_normal(size=(1,self.naff))
self.theta = self.expfac_theta * self.prev_theta -
(1-self.expfac_theta)

idx_spk = np.where(self.v>=self.theta)

self.S[n,idx_spk] = 1
self.theta[idx_spk] = self.theta[idx_spk] + self.b

self.prev_u = self.u
self.prev_theta = self.theta

--
James Snyder
Biomedical Engineering
Northwestern University
jbsnyder@gmail.com
```