nested for loop
Terry Reedy
tjreedy at udel.edu
Sun May 23 19:35:18 EDT 2004
"Dan Bishop" <danb_83 at yahoo.com> wrote in message
news:ad052e5c.0405231407.37ddba21 at posting.google.com...
> What you *can* do to make your code faster (if you don't change matr
> once it's created) is to precompute the 676 possible matrix rows.
>
> ELEMENT_RANGE = range(26)
> MATRIX_ROWS = [[x, y] for x in ELEMENT_RANGE
> for y in ELEMENT_RANGE]
> for row1 in MATRIX_ROWS:
> for row2 in MATRIX_ROWS:
> matr = [row1, row2]
>
> That takes only 532 ms -- almost 3 times faster than the original.
Cute. While I am quite familiar with and have experience applying the
principle of computing once instead of multiple times, I missed its
application here.
tjr
More information about the Python-list
mailing list