algorithm, optimization, or other problem?

Bas wegwerp at gmail.com
Tue Feb 21 11:59:35 EST 2006


Hi,

as the others have already said, move all the invariants out of the
loop. It is also not useful to randomly draw a vector out of random
array. Some other small things:

for i in range(len(x)): do something with x[i]
should probably be replaced by
for xi in x: do something with xi
this saves an array index operation, don't know if this works for a
numeric array

You use the y**2 twice. A good compiler might notice this, but Python
is interpreted...

use += instead of + for w, the calculation can be done in place, this
might save the creation of a new array/variable

I am not sure what you are doing with x, bit it seems that you are
transposing it a few times to many. Mightbe you can declare x as the
transpose of what you do now, thereby saving the transpose in the loop?

so my guess (not tested):

x=random((1000,100))    # 1000 input vectors,  declared differently
for xx in x:
             y=dot(xx,w)
             y2 = y*y
             w+=ETA*(y*xx-y2*w);
             th+= INV_TAU*(y2-th); 


Hope it helps,
Bas




More information about the Python-list mailing list