Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Improve the performance of a loop

Reply
Thread Tools

Improve the performance of a loop

 
 
querypk@gmail.com
Guest
Posts: n/a
 
      05-25-2005
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),(........) ]

 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      05-25-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      05-26-2005
(E-Mail Removed) 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

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

 
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
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
How to quickly improve your computer's performance @ Bona... Silverstrand Front Page News 0 08-24-2005 01:26 PM
Improve performance while reading from a BufferedReader. Nishi Bhonsle Java 1 07-20-2004 02:00 PM
Why do BufferedInputStream improve performance? Marc Twain Java 5 01-15-2004 07:17 PM
How to improve the asp.net application's performance? hb ASP .Net 2 11-25-2003 10:35 PM



Advertisments