[Numpy-discussion] Quick Question about Optimization

Robin robince@gmail....
Mon May 19 13:53:57 CDT 2008

On Mon, May 19, 2008 at 7:08 PM, James Snyder <jbsnyder@gmail.com> wrote:
>        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


The only thoughts I had were that depending on how 'vectorised'
random.standard_normal is, it might be better to calculate a big block
of random data outside the loop and index it in the same way as the
aff_input. Not certain if the indexing would be faster than the
function call but it could be worth a try if you have enough memory.

The other thing is there are a lot of self.'s there. I don't have a
lot of practicle experience, but I've read (
) that . based lookups are slower than local variables so another
thing to try would be to rebind everything to a local variable outside
the loop:
u = self.u
v = self.v
etc. which although a bit unsightly actually can make the inner loop
more readable and might speed things up.

The only other thing is be careful with things like this when
translating from matlab:
>            self.prev_u = self.u
since this is a reference not a copy of the data. This is OK because
when you recreate u as a product it creates a new object, but if you
changed u in another way ie self.u[:100] = 10 then self.prev_u would
still be pointing to the same array and also reflect those changes.
In this case it doesn't look like you explicitly need the prev_ values
so it's possible you could do the v and theta updates in place
(although I'm not sure if that's quicker)
u *= expfac_m
u += (1-expfac_m)*aff_input.. etc.
Of course you can also take the (1-)'s outside of the loop although
again I'm not sure how much difference it would make.

So sorry I can't give any concrete advise but I hope I've given some ideas...



More information about the Numpy-discussion mailing list