Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Another small filled circe sub (not as fast as fcirc)
#69
(08-31-2024, 02:18 PM)Dav Wrote: Lol, I never dream this thread would become so active.

A little tweaking test.  I thought removing the y = y + 1 in the WHILE/WEND in FC3 like below would speed it up a tad, but it didn't seem to.  In fact, this FOR/NEXT version usually comes in trailing behind the others on my laptop, but still fast.

- Dav

Code: (Select All)
Sub FCtweak (cx, cy, r, clr~&)
    r2 = r * r
    For y = 0 To r
        x = Sqr(r2 - y * y)
        Line (cx - x, cy + y)-(cx + x, cy + y), clr~&, BF
        If y <> 0 Then Line (cx - x, cy - y)-(cx + x, cy - y), clr~&, BF
    Next
    Line (cx - r, cy)-(cx + r, cy), clr~&, BF
End Sub

2 Things about the above:

1) a For Loop is the slowest loop structure in QB64 so use Do While, Do Until, While... or even a Goto before you use a For Loop
The jury is out whether While is slower than Do Until or Do While, anyone have proof?

2) Never put a decision in a Loop, if you don't have to, y = y+1 is much cheaper speed wise!
If y <> 0 Then ' <<< will slow down the code like crazy!!!

Comment Samuel has an updated version of FC3 that might be faster than mine?
I like how he got rid of the y = 1 statement with the Do Until
which might have been the Point Pete was trying to make with his insistence on Do While or Do Until

Samuel version of FC3 he called it FC_Dav but I was the one who made it two line statements per loop modding Dav's one line statement per loop. Lets' call it FC3 because after my mod Dav came back with y2 and now Samuel got rid of Y = 1 statement same as Dav's latest.
Code: (Select All)
Sub FC3 (cx As Long, cy As Long, r As Long, c As _Unsigned Long)
    $Checking:Off

    Dim r2 As Long: r2 = r * r
    Dim As Long x, y

    Line (cx - r, cy)-(cx + r, cy), c, BF

    Do While y < r
        y = y + 1

        x = Sqr(r2 - y * y)

        Line (cx - x, cy + y)-(cx + x, cy + y), c, BF
        Line (cx - x, cy - y)-(cx + x, cy - y), c, BF
    Loop

    $Checking:On
End Sub

Samuel put back the variable Declares all Long except Color and I am not convinced thats faster than all Single type which I found to be faster when I started playing around with Davs single Line statement per loop but he made everything Long by default. So going back to Single Type default mixes math and slows down results.

You need Long Type for R2 so make x, y Long too so you don't slow things by mixing math Types. This makes an FC3 sub more acceptable to Option Explicit specially if your overall default is Long and not Single as Samuel setup in his test code.
b = b + ...
Reply


Messages In This Thread
RE: Another small filled circe sub (not as fast as fcirc) - by bplus - 08-31-2024, 02:33 PM



Users browsing this thread: 28 Guest(s)