Nested loop limit?

Dan Christensen jdc at uwo.ca
Sun Jul 11 13:21:37 EDT 2004


Peter Hansen <peter at engcorp.com> writes:

> Dan Christensen wrote:
>
>> Well, in a computation in quantum gravity, I have the C code shown
>> below.  It has exactly 20 nested for loops!  There are of course
>> other ways to write it, but given the number of iterations, speed
>> is the bottom line.
>
> (Thanks for the _real_ real-world example. :-)
...
> its general appearance suggests to
> me that it could also be written, perhaps with equivalent or
> conceivably even better performance, and at least more generality,
> with some kind of table-driven approach.  I'll certainly bow
> to your better judgment if you've considered that possibility.

Yes, I've considered it, and in fact I'll be forced to use a
table-driven approach to handle more general situations, where the
geometry is read in from a file.  I haven't done timing comparisons,
but I would be very surprised if it was faster.  There would be some
beneficial cache effects from the shorter code, but more lookups and
index shuffling which I would expect to slow things down.  But maybe
I'm not familiar with some tricks that would make this technique
faster. 

As for maintainability of my method, using the macros means that
writing the code is very similar to filling in the table in the first
place!  :-)

I've actually toyed with the idea of having the program generate the
code (maybe in Python) on the fly, compile it (e.g. with psyco), and
then run it.  But with Python's limit of 20 nested loops, that won't
fly. 

Dan



More information about the Python-list mailing list