Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Today I learned loops are slow
#1
I needed a quick way to calculated the position of a sprite on a sprite sheet if I knew what number sprite it was.  Say you had a sheet that was 40x40 and you wanted the 22nd sprite on it. Something like that. 
I came up with two ways to get it, one was with a double for loop and the other was just using some multiplication.  It doesn't do anything with the xpos, ypos variables other than print the last two calculated.  To get the location of the sprite on the sheet you could just multiply these two numbers by the width and height of the sprite cells respectively.  This was just a speed test.  My second subroutine that relied on pure calculation won out.

Code: (Select All)

''Grid finder - locate a point on a grid from a linear number counting from the origin
''count could run right to left and up to down like pixels on a screen

Option _Explicit
Randomize Timer

''define grid parameters
Const GX = 500 ''width
Const GY = 500 ''height

''initialize points on the grid - load the array
Dim g_points&(30000)
Dim i As Long
For i = 0 To UBound(g_points&)
    g_points&(i) = Int(Rnd * (GX * GY)) + 1
Next

''setup
Dim start_time As Double
Dim end_time As Double
Dim xpos As Long
Dim ypos As Long

''double loop test
Print "Double Loop Test Start."
start_time = Timer

DoubleLoop g_points&(), xpos, ypos, GX, GY
Print "Last coordinates calculated: "; g_points&(UBound(g_points&)); xpos; ypos

end_time = Timer
Print end_time - start_time

''calculation test
Print "Calculation Test Start."
start_time = Timer

GridCalc g_points&(), xpos, ypos, GX, GY
Print "Last coordinates calculated: "; g_points&(UBound(g_points&)); xpos; ypos

end_time = Timer
Print end_time - start_time

Sleep
End


''------------------Subroutines-------------------
Sub DoubleLoop (points&(), xcoord&, ycoord&, gridsizex&, gridsizey&)

    ''find the position of a linear value on a grid using a double loop
    Dim i&
    Dim x&
    Dim y&
    Dim count&

    ''parse the array to get the values for each point
    For i& = 0 To UBound(points&)

        For y& = 0 To gridsizey& - 1
            For x& = 0 To gridsizex& - 1
                ''Calculate the position of the value on the grid
                count& = count& + 1
                If count& = points&(i&) Then
                    xcoord& = x&
                    ycoord& = y&
                    ''reset the count
                    count& = 0

                    GoTo xyloop_exit
                End If
            Next x&
        Next y&
        xyloop_exit:

    Next i&

End Sub ''DoubleLoop


Sub GridCalc (points&(), xcoord&, ycoord&, gridsizex&, gridsizey&)

    ''find the position of a linear value on a grid using math
    Dim i&

    ''parse the array to get the values for each point
    For i& = 0 To UBound(points&)
        ycoord& = Int(points&(i&) / gridsizey&)
        xcoord& = points&(i&) - (ycoord& * gridsizex&) - 1 ''start at 0
    Next i&

End Sub ''GridCalc
Reply


Messages In This Thread
Today I learned loops are slow - by CMR - 11-27-2025, 05:20 AM
RE: Today I learned loops are slow - by bplus - 11-27-2025, 02:09 PM
RE: Today I learned loops are slow - by CMR - 11-27-2025, 05:20 PM
RE: Today I learned loops are slow - by bplus - 11-27-2025, 06:27 PM
RE: Today I learned loops are slow - by bplus - 11-27-2025, 07:17 PM
RE: Today I learned loops are slow - by CMR - 11-27-2025, 08:35 PM
RE: Today I learned loops are slow - by bplus - 11-27-2025, 09:17 PM
RE: Today I learned loops are slow - by Pete - 11-27-2025, 10:52 PM
RE: Today I learned loops are slow - by CMR - 11-28-2025, 01:58 AM
RE: Today I learned loops are slow - by TDarcos - 12-20-2025, 02:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I have learned something new: how to get a run-time syntax error TDarcos 2 970 10-02-2024, 07:04 PM
Last Post: TDarcos
  A little bit of spark fun today TerryRitchie 2 839 05-18-2024, 12:59 AM
Last Post: bobalooie
  Loops alternate recursive ways bplus 10 2,036 02-01-2023, 02:05 PM
Last Post: Dimster

Forum Jump:


Users browsing this thread: 1 Guest(s)