473,480 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Improve the performance of a loop

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),(........) ]

Jul 19 '05 #1
3 1492
qu*****@gmail.com 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
Jul 19 '05 #2
qu*****@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

Jul 19 '05 #3
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.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
3450
by: Brian Patterson | last post by:
I have noticed in the book of words that hasattr works by calling getattr and raising an exception if no such attribute exists. If I need the value in any case, am I better off using getattr...
10
13807
by: Roy Smith | last post by:
I've been running some benchmarks to compare streams and stdio performance. I've always suspected stdio was faster, but was astonished to discover how much faster. I timed the following running...
10
3100
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x";...
17
2057
by: Sean Kenwrick | last post by:
I am writing a byte-code interpreter/emulator for a language that exclusively uses strings for variables (i.e all variables are pushed onto the stack as strings). Therefore for arithmetic...
9
4594
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
23
1802
by: philipl | last post by:
hi, I have some code here which basically look for within a string, the occurance of any 3 consectative characters which are the same. so AAA bbb etc would be reported by this function. I later...
2
2760
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000...
3
3623
by: oravm | last post by:
Hi, I re-write a query and used bulk collect to improve the performance of the batch process. The query below has NO compile error but when execute query there is error 'ORA-01403: no data...
5
1496
by: Gilles Ganault | last post by:
Hello I'm no PHP expert, and I'm reading "Building scalable web sites". In the tips section, the author mentions using templates to speed things up. I was wondering how the template engines...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.