Improve the performance of a loop

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

  1. Guest

    What is the fastest way to code this particular block of code below..
    I used numeric for this currently and I thought it should be really
    fast..
    But, for large sets of data (bx and vbox) it takes a long time and I
    would like to improve.

    vbox = array(m) (size: 1000x1000 approx)
    for b in bx:
    vbox[ b[1], b[0]:b[2] ] = 0
    vbox[ b[3], b[0]:b[2] ] = 0
    vbox[ b[1]:b[3], b[0] ] = 0
    vbox[ b[1]:b[3], b[2] ] = 0

    and vbox is a 2D array
    where bx is of form [( int,int,int,int),(........) ]
     
    , May 25, 2005
    #1
    1. Advertising

  2. wrote:
    > vbox = array(m) (size: 1000x1000 approx)
    > for b in bx:
    > vbox[ b[1], b[0]:b[2] ] = 0
    > vbox[ b[3], b[0]:b[2] ] = 0
    > vbox[ b[1]:b[3], b[0] ] = 0
    > vbox[ b[1]:b[3], b[2] ] = 0


    This may not help, but you could try to minimize the number of times you
    call b's __getitem__, e.g.:

    for b0, b1, b2, b3 in bx:
    vbox[b1, b0:b2] = 0
    vbox[b3, b0:b2] = 0
    vbox[b1:b3, b0] = 0
    vbox[b1:b3, b2] = 0

    If it helps, I wouldn't expect it to help a lot though. You could also try:

    for b0, b1, b2, b3 in bx:
    b0_2 = slice(b0, b2)
    b1_3 = slice(b1, b3)
    vbox[b1, b0_2] = 0
    vbox[b3, b0_2] = 0
    vbox[b1_3, b0] = 0
    vbox[b1_3, b2] = 0

    But again, I wouldn't expect dramatic improvements...

    STeVe
     
    Steven Bethard, May 26, 2005
    #2
    1. Advertising

  3. Peter Otten Guest

    wrote:

    > What is the fastest way to code this particular block of code below..
    > I used numeric for this currently and I thought it should be really
    > fast..
    > But, for large sets of data (bx and vbox) it takes a long time and I
    > would like to improve.
    >
    > vbox = array(m) (size: 1000x1000 approx)
    > for b in bx:
    > vbox[ b[1], b[0]:b[2] ] = 0
    > vbox[ b[3], b[0]:b[2] ] = 0
    > vbox[ b[1]:b[3], b[0] ] = 0
    > vbox[ b[1]:b[3], b[2] ] = 0
    >
    > and vbox is a 2D array
    > where bx is of form [( int,int,int,int),(........) ]


    You can try

    for b0, b1, b2, b3 in bx:
    vbox[b1:b3, b0:b2+1:b2-b0] = 0
    vbox[b1:b3+1:b3-b1, b0:b2] = 0

    By the way, the bottom/right cell of your box remains nonzero. Is that
    intentional?

    Peter
     
    Peter Otten, May 26, 2005
    #3
  4. Guest

    Actually slicing the way you suggested improved it to some extent. I
    did profile on this and I observed that it reduced the number of calls
    for __get_item__ and improved the timing to some extent. Which was
    useful to some extent.

    Thanks again.
     
    , May 26, 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. hb
    Replies:
    2
    Views:
    530
  2. Marc Twain
    Replies:
    5
    Views:
    4,890
    Andrew Thompson
    Jan 15, 2004
  3. Nishi Bhonsle
    Replies:
    1
    Views:
    951
    Thomas Weidenfeller
    Jul 20, 2004
  4. Roy Smith
    Replies:
    10
    Views:
    845
    Krzysztof Rzymkowski
    Nov 24, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    458
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page