Active Forums > Programs
Better Bench by Ed Davis
bplus:
Generic Basic code convert to QB64 by me for timing and Types:
--- Code: QB64: ---$Checking:OffDefLng A-ZDim As Double x, y, xx, yy, startstart = Timer(.001)accum = 0count = 0While count < 1545 leftedge = -420 rightedge = 300 topedge = 300 bottomedge = -300 xstep = 7 ystep = 15 maxiter = 200 y0 = topedge While y0 > bottomedge x0 = leftedge While x0 < rightedge y = 0 x = 0 thechar = 32 xx = 0 yy = 0 i = 0 While i < maxiter And xx + yy <= 800 xx = Int((x * x) / 200) yy = Int((y * y) / 200) If xx + yy > 800 Then thechar = 48 + i If i > 9 Then thechar = 64 End If Else temp = xx - yy + x0 If (x < 0 And y > 0) Or (x > 0 And y < 0) Then y = (-1 * Int((-1 * x * y) / 100)) + y0 ' << this line was revised in later post Else y = Int(x * y / 100) + y0 End If x = temp End If i = i + 1 Wend x0 = x0 + xstep accum = accum + thechar Wend y0 = y0 - ystep Wend If count Mod 300 = 0 Then Print accum, End If count = count + 1Wend Print accumPrint Timer(.001) - start; " seconds" 'This is the output: ' 200574 60372774 120544974 180717174 240889374 301061574 309886830 I added DEFLNG A-Z and Dim as Double x, y, xx, yy
and I got below 10 secs on Timer with $Checking:Off, 9.61 was best time with everything turned off including Wifi.
Ed has QB64 down for 9.35 secs maybe on better machine...
http://basic4all.epizy.com/index.php?topic=21.msg166#msg166
Can anyone make a significant drop? FreeBasic at top at 1.09 secs
SMcNeill:
y = (-1 * Int((-1 * x * y) / 100)) + y0 <-- isn't a negative * negative = positive? How is this any different from the ELSE case?
bplus:
Honestly I haven't done any analysis of code yet. I just know the first time I tried I didn't get what I was supposed to and when I made the substitution, the output came out right.
Can FB be so much faster?
Hey! Ed's here!
bplus:
--- Quote from: SMcNeill on April 11, 2022, 04:41:57 pm --- y = (-1 * Int((-1 * x * y) / 100)) + y0 <-- isn't a negative * negative = positive? How is this any different from the ELSE case?
--- End quote ---
looks to me like this forces neg when one or the other is out of bounds but not both.
EDIT
Ed Davis:
--- Quote from: SMcNeill on April 11, 2022, 04:41:57 pm --- y = (-1 * Int((-1 * x * y) / 100)) + y0 <-- isn't a negative * negative = positive? How is this any different from the ELSE case?
--- End quote ---
Note this funky code:
--- Code: --- if (x < 0 and y > 0) or (x > 0 and y < 0) then
y = (-1 * Int((-1 * x * y) / 100)) + y0
else
y = int(x * y / 100) + y0
end if
--- End code ---
When I originally developed this, I could not get the same results with Python that I got with C.
It turns out that there is no consensus as to whether integer division, when one of the operands is negative, should round towards zero, negative infinity, or positive infinity.
Python and Ruby both round towards negative infinity. C rounds towards zero.
In order to get the same output from each language (to verify that each language was essentially computing the same thing and doing similar work), I had to figure out how to preclude one of the operands from being negative.
The code checks, and if either x or y is < 0 (but not both), then it multiplies by minus one to force positive division, and then by minus one again at the end to restore the sign.
And I agree, there is probably a better way to do this. But at the time, I just wanted a solution that worked, and that is what my little mind came up with.
Note that later on, I found out that Python had a integer divide operator, so there was no need to go through the hoops I did. But, since I had trouble with my "and" and "or" logic and precedence in my own interpreter, I left it in as a sort of unit test :) And, it does give whatever language is doing this more work to chew on, and more opportunities for optimization.
Navigation
[0] Message Index
[#] Next page
Go to full version