my loop is too slow

Christian Tismer tismer at appliedbiometrics.com
Mon May 10 12:27:11 EDT 1999


"Michael P. Reilly" wrote:

[142**4 complex expressions]

> Each time through each loop a list of length 142 is created.  This
> means that for the h loop alone a new list is created 2863288 times.
> loop  #created  calculation
>    k         1  pow(142, 0)
>    i       142  pow(142, 1)
>    j     20164  pow(142, 2)
>    h   2863288  pow(142, 3)

[just using one dim range]

> : This will save you about 406*10E6 list creations.

Well, that's nice, but it doesn't consume the time.
Also I doubt your number, since you can save

2883595 -1 list creations (have to create it once).

What we actually have here is the expression

      a[k,i]=a[k,i]+c[k,h]*c[k,j]*f[j,i]*f[i,h]

which is executed 406586896 times. The list overhead is
completely neglectible. I tested with a length of 30,
and it was about 2 percent.

The expression contains four complex operations, giving four
calls to malloc, since complex numbers have no internal
memory caching support. 
Now, in relation to the one malloc for the list, these are
another 568 malloc calls into the operating system.
There goes the speed.

Test result for len=30, matrices are real, 

inner list is copied:   8.79 sec
inner list not copied:	8.51 sec

Test result for len=30, matrices are complex, 

inner list not copied:	15.71 sec

This is for sure not the computational complexity
of complexes, but malloc.
But also just a factor of two, which tells us that we should
really not expect about 1600 million calculations
to be a cakewalk for Python.

> I haven't ever needed to look into it, but it sounds like some of the
> features of NumPy might help you.  I'm sure that someone who knows more
> about it can help more. :)

I believe NumPy is the only way to avoid C programming.

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list