Solved

Speed: Need more!

Posted on 1998-11-17
3
136 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now