list math for version 1.5.2 without Numeric

Fernando Pérez fperez528 at yahoo.com
Tue Nov 27 16:04:00 EST 2001


J.Jacob wrote:

> List comprehension in the later Python versions is fun but i need to
> get this piece of code to run as quickly as possible on Python
> version 1.5.2 and no NumPy available (not allowed to install extra
> things on the computers here alas).
> 
> This is the code as it is now, it works allright but the profiler
> says we spend a lot of time here:
> for j in range(self.nh):
>     sum = 0.0
>     for i in range(self.ni):
>         sum = sum + self.ai[i] * self.wi[i][j]
> 
> the .nh and .ni values are correct
> I have been trying things like
> 
> import operator
> reduce(operator.add, map( operator.mul, ... ))
> 
> but i am not at all used to optimization wizardry so i have no idea
> if i can find a good solution.
> Any expert wants to help ?

Sorry to be blunt, but if you really do need speed (and you seem 
quite adamant about it), you *need* NumPy. This thing executing as a 
python loop will take ages for large matrices, while in NumPy it runs 
as a C loop without tpyechecking at every step. There's simply no 
comparison.

I understand you may have sysadmin limitations, but you must have at 
least a personal directory somewhere (if not, are you editing in 
memory only? :). Just put numpy there, and add the path to sys.path. 
It won't be available system wide but you'll be able to work.

And I'd double check the order of your indices just to be safe: you 
are doing sum_elements(wi_transpose*ai), where wi is a matrix and ai 
a vector. Are you sure you need the transpose? If you do, it will 
cost you even in Numpy to do it. The only fast way out is to write it 
in C where you can write the indices any way you want.

Cheers,

f



More information about the Python-list mailing list