performance of Nested for loops

Andrew Dalke dalke at dalkescientific.com
Sat May 21 00:56:01 EDT 2005


querypk wrote:
> Is there a better way to code nested for loops as far as performance is
> concerned.
> 
> what better way can we write to improve the speed.
> for example:
> N=10000
> for i in range(N):
>    for j in range(N):
>        do_job1
>    for j in range(N):
>        do_job2

For this case compute the range once

range_10000 = range(10000)
for i in range_10000:
  for j in range_10000:
    do_job1()
  for j in range_10000:
    do_job2()

Using xrange(10000) may be faster but you would need to test
that out for your case.

				Andrew
				dalke at dalkescientific.com




More information about the Python-list mailing list