Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > performance of Nested for loops

Reply
Thread Tools

performance of Nested for loops

 
 
querypk@gmail.com
Guest
Posts: n/a
 
      05-20-2005
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

 
Reply With Quote
 
 
 
 
Charles Krug
Guest
Posts: n/a
 
      05-20-2005
On 20 May 2005 15:35:10 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)>
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
>


What do you see when you profile the code?

Premature Optimization is the root of all manner of evil and all that
good stuff.

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      05-21-2005
You can use xrange(N) that way Python doesn't have
to build the 10000 item lists 20000 times. Other than
that one would need to know why you would call do_job1
and do_job2 10000 times each inside a 10000 iteration
loop. Most VERY large performance gains are due to
better algorithms not code optimization.

Larry Bates


(E-Mail Removed) 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
>

 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      05-21-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
(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
nested loops and conditional statements Porthos XML 3 02-07-2005 08:47 PM
dynamic number of nested loops Allan Bruce Java 5 07-03-2004 09:12 PM
break or continue out of nested loops viza C Programming 5 07-17-2003 04:04 AM
compacting similar nested for loops SplaTTer C++ 2 07-02-2003 09:16 PM



Advertisments