?
Solved

Speed: Need more!

Posted on 1998-11-17
3
Medium Priority
?
151 Views
Last Modified: 2010-05-03
I have a quite large loop in my program and want to increase the speed at which it runs... Is there any way to do this? The code is below. Although you might want to copy it into wordpad... It may be hard to read otherwise. All varables are delcared as best and small as they could be. Thanks!


For xnow = 0 To XMAPSHOWN * (tile_width + 2) Step tile_width
tilex = tilex + 1
tiley = ycord
For ynow = 0 To ymapshown * (tile_Height + 2) Step tile_Height
tilenow = map(tilex, tiley).land1

If tilenow < 1000 Then GoTo no_draw
If tilenow >= 1000 And tilenow <= 1225 Then page = 1
If tilenow >= 2000 And tilenow <= 2225 Then page = 2
If tilenow >= 3000 And tilenow <= 3225 Then page = 3
If tilenow >= 4000 And tilenow <= 4225 Then page = 4
If tilenow >= 5000 And tilenow <= 5225 Then page = 5
If tilenow >= 6000 And tilenow <= 6225 Then page = 6
If tilenow >= 7000 And tilenow <= 7225 Then page = 7
If tilenow >= 8000 And tilenow <= 8225 Then page = 8
If tilenow >= 9000 And tilenow <= 9225 Then page = 9

tile = (tilenow - (page * 1000)) - 1
i = BitBlt(mapeditor.Picture1.hdc, xnow, ynow, tile_width, tile_Height, Graphics.Graphics_pages(page + 9).hdc, ((tile Mod temtiles_ud) * (tile_width + 2)) + 1, ((tile \ temtiles_rl) * (tile_Height + 2)) + 1, SRCAND)
i = BitBlt(mapeditor.Picture1.hdc, xnow, ynow, tile_width, tile_Height, Graphics.Graphics_pages(page).hdc, ((tile Mod temtiles_ud) * (tile_width + 2)) + 1, ((tile \ temtiles_rl) * (tile_Height + 2)) + 1, SRCINVERT)
no_draw:

tiley = tiley + 1
Next ynow
Next xnow
Exit Sub
0
Comment
Question by:testing38
3 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 200 total points
ID: 1445338
c1 = (tile_height + 2) / temtiles_rl
c2 = (tile_width + 2)

For xnow = 0 To XMAPSHOWN * (tile_width + 2) Step tile_width
tilex = tilex + 1
tiley = ycord
For ynow = 0 To ymapshown * (tile_Height + 2) Step tile_Height
tilenow = map(tilex, tiley).land1

If tilenow < 1000 Then GoTo no_draw

page = int(tilenow/1000)
tile = (tilenow mod 1000) - 1
c3 = ((tile Mod temtiles_ud) * c2) + 1_
c4 = tile * c1 + 1

i = BitBlt(mapeditor.Picture1.hdc, xnow, ynow, tile_width, tile_Height, _
  Graphics.Graphics_pages(page + 9).hdc, c3, c4, SRCAND)

i = BitBlt(mapeditor.Picture1.hdc, xnow, ynow, tile_width, tile_Height,_
  Graphics.Graphics_pages(page).hdc, c3, c4, SRCINVERT)
no_draw:

tiley = tiley + 1
Next ynow
Next xnow
Exit Sub


You were performing the same calculations more than once within the loop.  This eats up a lot of time.  So I added a couple of variables to store them after they are calculated once (c1,c2,c3,c4).

I could not test this, because I do not have Visual Basic.  You might have to check the syntax because there might be something I didn't catch (like mismatched parentheses, etc.)

Also, I don't understand why you didn't set the page for values outside the 0 to 225 range.  I took this check out, because your program didn't seem to need it anyway.
0
 

Author Comment

by:testing38
ID: 1445339
Thanks this worked great with a little editing!
0
 

Expert Comment

by:path
ID: 1445340
Just another thought you may find useful - on your iterative "next", comment out (but leave it for readability) the counter name ie Next 'ynow (comment) and Next 'xnow(comment) - this will improve the speed considerable, expecially since there are two nested loops running. Simple but effective.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question