Solved

Speed: Need more!

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

707 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

16 Experts available now in Live!

Get 1:1 Help Now