list math for version 1.5.2 without Numeric

Chris Barker chrishbarker at home.net
Thu Nov 29 14:13:15 EST 2001


"J.Jacob" wrote:
> But, i managed to get a nice speedup !  And a surprising result.

> This version takes 101 seconds in my test program.

> This took 76 seconds for the test, a big improvement !

> This also takes 76 seconds doing the test.

> This version takes 94 seconds.

> This version takes 90.4 seconds.

> Taking 68.7 seconds.

you took a whole lot of time to get from 101 seconds to 68.7 seconds. I
would venture top guess that if 68.7 seconds were "fast enough" then 101
seconds would probably be fast enough as well. Personally, I look for a
factor of 5 to 10 speed-up at least in order for it to worth a lot of
effort to optimize.

By the way, your original code:

for j in range(self.nh):
    sum = 0.0
    for i in range(self.ni):
        sum = sum + self.ai[i] * self.wi[i][j]

Are you sure you want to re-set sum inside the j loop? you are
re-defining it, so unless you store it, you are only going to get the
sum of the last row, which makes it pretty silly to loop through the
whole mess.

Anyway, here is the code for Numeric. You must be using some pretty slow
hardware, or massive arrays to get times as long as you get!

With and without Numeric:

with loops it took 39.060000 seconds
with Numeric it took 2.480000 seconds

Now that is a speed-up that is worth it!

The truth is that Python will never do numeric work quickly without the
Numeric module. It might be less time to figure out how to install
Numeric than to try to optimize raw Python for limited gain. Here is the
Numeric code, which is also nice and clean, compact and easy to read:

from Numeric import *
import RandomArray
import time

nh = 5000
ni = 1000

ai = RandomArray.uniform(0,100,(ni,))
wi = RandomArray.uniform(0,100,(nh,ni))             

start = time.clock()
for j in range(nh):
    total = 0.0
    for i in range(ni):
        total = total + ai[i] * wi[j][i]
print "with loops it took %f seconds"%(time.clock()-start)


start = time.clock()

totals = sum((ai*wi),1) # this is an array of each row total

print "with Numeric it took %f seconds"%(time.clock()-start)

grand_total = sum((ai*wi).flat) # this is the sum of all of them






-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list