my loop is too slow

Fredrik Lundh fredrik at pythonware.com
Mon May 10 05:41:50 EDT 1999


SC Zhong <sai at dft.chem.cuhk.edu.hk> wrote:
> I tried to learn and use python a week ago, and wrote
> a small application.  When it run the loop:
> 
> for k in range(dimension):
> for i in range(dimension):
> for j in range(dimension):
> for h in range(dimension):
> a[k,i]=a[k,i]+c[k,h]*c[k,j]*\
> f[j,i]*f[i,h]
> 
> it took very long time (may be 10 hours in my HP workstation). dimension
> is 142 and a,c,f are all complex.  
>
> I do not want to waste the other part of the program which processing text
> file and rewrite the whole program in C or Fortran.  Is there ways to speed
> the above up?

consider using the numerical Python extension:
http://www.python.org/topics/scicomp/

another way is to rewrite just the speed-critical part in C.
http://www.python.org/doc/current/ext/ext.html
http://www.python.org/doc/current/api/api.html
http://www.swig.org/

</F>





More information about the Python-list mailing list