Improve the performance of a loop

Peter Otten __peter__ at web.de
Thu May 26 02:38:05 EDT 2005


querypk at gmail.com 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




More information about the Python-list mailing list