04-17-2012, 01:27 AM
Oh no. It does more like 65,000 (it's actually doing 250 loops of 250 values) of them in under 7 seconds with the new version, actually.
and yes, if you did it in basic, using the standard formula of SQR (x^2 + y^2) you'd definitely be well over 2 hours. Probably a LOT more than 2 hours, given the other overheads of basic.
This doesn't actually calculate SQR (X^2+y^2); but does it a completely different way - and it only gives an estimate, not a perfect result. This is probably "close enough" for most games, and as you can see, a lot faster.
You can get a guaranteed "closer than 1" answer, if you use the iSqrt routine to calculate isqrt(x^2+y^2) - because isqrt gives you the whole number part of the square root. (so isqrt(9)=3, but isqrt(10)=3 as well). Using this routine tested in at about 50 seconds for the 65,000 tests.
Finally, there's fsqrt in the wiki library. This did the job in 44 minutes; about 1/3 the time of the ROM sqr routine. fsqrt gives exactly the same answer as the rom routine - in fact it even uses the rom calculator, but it uses a much better method of getting a square root. You get the full floating point answer in something like 1/3 to 1/6 of the time.
The real problem is the ROM SQR routine is just very inefficient. But, to be fair to the zx spectrum designers, it's really small too. 7 bytes, if I recall.
and yes, if you did it in basic, using the standard formula of SQR (x^2 + y^2) you'd definitely be well over 2 hours. Probably a LOT more than 2 hours, given the other overheads of basic.
This doesn't actually calculate SQR (X^2+y^2); but does it a completely different way - and it only gives an estimate, not a perfect result. This is probably "close enough" for most games, and as you can see, a lot faster.
You can get a guaranteed "closer than 1" answer, if you use the iSqrt routine to calculate isqrt(x^2+y^2) - because isqrt gives you the whole number part of the square root. (so isqrt(9)=3, but isqrt(10)=3 as well). Using this routine tested in at about 50 seconds for the 65,000 tests.
Finally, there's fsqrt in the wiki library. This did the job in 44 minutes; about 1/3 the time of the ROM sqr routine. fsqrt gives exactly the same answer as the rom routine - in fact it even uses the rom calculator, but it uses a much better method of getting a square root. You get the full floating point answer in something like 1/3 to 1/6 of the time.
The real problem is the ROM SQR routine is just very inefficient. But, to be fair to the zx spectrum designers, it's really small too. 7 bytes, if I recall.