list math for version 1.5.2 without Numeric

J.Jacob joost_jacob at hotmail.com
Thu Nov 29 07:47:24 EST 2001


Thank you (and the other posters) for the replies !

Using Numeric would be great but has to wait until it is included
in a python distribution and they install that new version here.
All these computers have Python 1.5.2 and some have no network
connection so i cannot link to numeric at my computer or
something like that.  Maybe i should supply Numeric with the
program but i try to avoid that.  Coding part in C is no option
since the platform is unknown, they have winNT, linux and
solaris.

But, i managed to get a nice speedup !  And a surprising result.


Here is my optimization story:

The original code:
version_0
        for j in range(self.nh):
            sum = 0.0
            for i in range(self.ni):
                sum = sum + self.ai[i] * self.wi[i][j]
This version takes 101 seconds in my test program.

Doing your Andrew's tips about avoiding lookups i got:
version_1
        range_ni = range(self.ni)
        range_nh = range(self.nh)
        ai = self.ai
        wi = self.wi
        for j in range_nh:
            sum = 0.0
            for i in range_ni:
                sum = sum + ai[i] * wi[i][j]
This took 76 seconds for the test, a big improvement !

Next thing to try was to transpose the "wi" indexes and keep all
my other code working.  Transposing the indices in the code was
difficult (complex neural network update function) since some
other code had to be changed too.  The original was bpnn.py i
found on internet, by Neil Schemenauer (thank you if you read
this!).  But pffff, finally i had it done (and did testing) and
now i could translate the original code to:

version_2
        for j in range_nh:
            sum = 0.0
            for i in range_ni:
                sum = sum + ai[i] * wi[j][i]
This also takes 76 seconds doing the test.

But now we can do the reduce()/map() stuff! C-loops! FAST!! hurray!

version_3
        for j in range_nh:
            sum = reduce(operator.add, map(operator.mul, ai, wi[j]))

This version takes 94 seconds.
:((((

I can do better:
version_4
        for j in range_nh:
            wij = wi[j]
            sum = reduce(operator.add, map(operator.mul, ai, wij))
This version takes 90.4 seconds.
Argh !  Oh well at least they produce the same output in my
testprogram as the other versions so i am pretty sure the code is
semantically correct.


And the winner is:
version_5
        for j in range_nh:
            wij = wi[j]
            sum = 0.0
            for i in range_ni:
                sum = sum + ai[i] * wij[i]
Taking 68.7 seconds.
This is like version_2 but with using "wij" to avoid some
indexing.

Ok i have a faster version now, but i did not like the reduce/map
adventure at all hehehe.  I read in another post that Guido does
not like to reduce() either and this could be just another example
of....<fill-in-yourself>
I sure hope i do not have a nasty bug hidden somewhere but that
seems unlikely since all the versions produce the same result when
using the neural network.

----------------------------------------------------------------
"I have a problem: my Python for-loop is too slow.  Oh wait i can
use reduce() and map() and aww that will be cool!"
<now-you-have-two-problems>



More information about the Python-list mailing list