Quote Originally Posted by si_the_geek View Post
The speed of VB programs can usually be very close to (or even equal to) the speed of C programs, assuming both have been optimised properly (if not, whichever is most optimised will probably be faster).

I picked a random set of loops from your code ("Case 4" of LoadRawImageData), and it is clear that they are unoptimised:
Code:
    For BlockY = 0 To HScroll2.Value - 1 Step TileSize
      For BlockX = 0 To HScroll1.Value - 1 Step TileSize
        For SubY = 0 To TileSize - 1
          For SubX = 0 To TileSize - 1
            Y = BlockY + SubY
            X = BlockX + SubX
            ImgData(2, X, HScroll2.Value - 1 - Y) = Pal(RawData(n)) And &HFF
            ImgData(1, X, HScroll2.Value - 1 - Y) = (Pal(RawData(n)) And &HFF00&) / &H100&
            ImgData(0, X, HScroll2.Value - 1 - Y) = (Pal(RawData(n)) And &HFF0000) / &H10000
            n = n + 1
          Next SubX
        Next SubY
      Next BlockX
    Next BlockY
(indenting added to make the code much more readable, and make the issues clearer)

The value of Y does not change for different values of SubX, so why calculate it repeatedly inside the SubX loop? You are doing it TileSize times as often as you need to, which can be eliminated by simply moving that line up to the SubY loop.

There are many other things which are being done more often than needed, one of the best examples being the calculation of HScroll2.Value - 1, which presumably doesn't change at all while these loops are running... in which case you are doing it 3 * HScroll1.Value * HScroll2.Value times as often as you need to.

Add in a few other similar things, and it can become this (untested):
Code:
    Dim H1 as Long, H2 as Long
    Dim PR as Long
    Dim TileSizeMinus1 as Long

    H1 = HScroll1.Value - 1
    H2 = HScroll2.Value - 1
    TileSizeMinus1 = TileSize - 1

    For BlockY = 0 To H2 Step TileSize
      For BlockX = 0 To H1 Step TileSize
        For SubY = 0 To TileSizeMinus1
          y = H2 - (BlockY + SubY)
          For SubX = 0 To TileSizeMinus1
            X = BlockX + SubX
            PR = Pal(RawData(n))
            ImgData(2, X, Y) = PR And &HFF
            ImgData(1, X, Y) = (PR And &HFF00&) \ &H100&
            ImgData(0, X, Y) = (PR And &HFF0000) \ &H10000
            n = n + 1
          Next SubX
        Next SubY
      Next BlockX
    Next BlockY
(this may have a minor change of behaviour due to using \ rather than / , so you may want to revert that and sacrifice the speed gain it gives)

For larger values of HScroll1.Value and HScroll2.Value, these changes should make a significant difference to the speed of these loops.

There are likely to be many more optimisations that can be done outside of the loops (including altering the data types of the variables that are used in the loops), even without considering a change of methodology (which often gives very dramatic improvements) or compiler options (which can also be dramatic).

Whether or not the speed can reach the same as an equivalent program is unpredictable (especially when you don't have the code for the other), and how much effort it is worth putting in to optimisation is up to you... in most cases reaching the ultimate speed isn't that important.


By the way, rather than linking to code on an external site, please post it in (or attach it to) your post... that way your thread doesn't become worthless if the other site isn't working, or if they delete/move the file, or someone is behind a company firewall which blocks that site, etc.

What does \ do in VB6 that / doesn't do in VB6? I never even saw the \ symbol before. What does it do? Are you referencing something from VB.net? Because you see I'm not using .net, but rather am using VB6 for this.