performance of Nested for loops

Discussion in 'Python' started by querypk@gmail.com, May 20, 2005.

  1. Guest

    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
    , May 20, 2005
    #1
    1. Advertising

  2. Charles Krug Guest

    On 20 May 2005 15:35:10 -0700, <>
    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.
    Charles Krug, May 20, 2005
    #2
    1. Advertising

  3. Larry Bates Guest

    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


    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
    >
    Larry Bates, May 21, 2005
    #3
  4. Andrew Dalke Guest

    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
    Andrew Dalke, May 21, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Bruce

    dynamic number of nested loops

    Allan Bruce, Jul 1, 2004, in forum: Java
    Replies:
    5
    Views:
    9,721
    Tor Iver Wilhelmsen
    Jul 3, 2004
  2. Porthos
    Replies:
    3
    Views:
    474
    Joris Gillis
    Feb 7, 2005
  3. SplaTTer
    Replies:
    2
    Views:
    369
    SplaTTer
    Jul 2, 2003
  4. Me
    Replies:
    2
    Views:
    229
  5. Fabian Vilers

    Performance on "for" loops

    Fabian Vilers, Feb 6, 2006, in forum: Javascript
    Replies:
    5
    Views:
    99
    Randy Webb
    Feb 7, 2006
Loading...

Share This Page