I was informed that my BASIC program is not very efficient, mainly due to the use of integer variables; all arithmetic operations in BASIC are performed on floating point numbers. Also, since the modulus is the max signed value for a byte (255), we can replace the whole operation with a faster AND. 10 A=53280:M=255 20 POKEA,M AND PEEK(A)+1:GOTO 20 This is "visually" about 3x faster, but still no where near as fast as the ML routine.
Machine code as I understood it to be called mystified me when I was a kid. I've made some progress since but spent many happy hours programming in BASIC. 👍
Probably the fastest way to do this in BASIC with one loop is: 10 B=53280 20 FOR I=0 TO 1023:POKEB,IANDN:NEXT This takes 308 jiffies. You could also do it with nested loops: 10 B=53280:N=255 20 FOR R=1 TO 4:FORI=.TON:POKEB,I:NEXT:NEXT This takes 205 jiffies. For comparison, here's an instrumented revised version (can't really time a GOTO loop): 10 B=53280:N=255 20 FORR=1TO1024:POKEB,N AND PEEK(B)+1 25 NEXT This takes 453 jiffies.
@@szulat Actually, it should be x = x AND 255, which eliminates the division without introducing a conditional, but I was trying to keep the operation close to the original.
BASIC interpretation is generally slow, but suitable for certain applications. For reference, "8-Bit Show And Tell" has a library of videos showing how to exploit BASIC to get speed gains. th-cam.com/users/8BitShowAndTell
actually this test does not show anything about basic speed. executing floating point calculations from ML would be exactly as slow as in basic (and btw. this floating point math is coded in ML... oh wait, shouldn't it be very fast then? 🤪)
I was informed that my BASIC program is not very efficient, mainly due to the use of integer variables; all arithmetic operations in BASIC are performed on floating point numbers. Also, since the modulus is the max signed value for a byte (255), we can replace the whole operation with a faster AND.
10 A=53280:M=255
20 POKEA,M AND PEEK(A)+1:GOTO 20
This is "visually" about 3x faster, but still no where near as fast as the ML routine.
Commodore BASIC is pretty crappy when it comes to efficiency. It might be interesting to compare some third-party BASICs to the built-in interpreter.
Machine code as I understood it to be called mystified me when I was a kid. I've made some progress since but spent many happy hours programming in BASIC. 👍
That's why I switched to C and then eventually I'll add some optimized ML routines.
Probably the fastest way to do this in BASIC with one loop is:
10 B=53280
20 FOR I=0 TO 1023:POKEB,IANDN:NEXT
This takes 308 jiffies.
You could also do it with nested loops:
10 B=53280:N=255
20 FOR R=1 TO 4:FORI=.TON:POKEB,I:NEXT:NEXT
This takes 205 jiffies.
For comparison, here's an instrumented revised version (can't really time a GOTO loop):
10 B=53280:N=255
20 FORR=1TO1024:POKEB,N AND PEEK(B)+1
25 NEXT
This takes 453 jiffies.
It should be mod 256, not 255.
Indeed it should! The last color isn't getting its fair share of cycles :)
no, it should be just "if x > 255 then x = 0", executing floating point division and multiplication is absurd
@@szulat Actually, it should be x = x AND 255, which eliminates the division without introducing a conditional, but I was trying to keep the operation close to the original.
@@johnsensebe3153 👍 yes, "and 255" would be better and also close to what 8 bit cpu actually does in "inc"
Is Microsoft BASIC really bad or is the interpretation just always going to be that slow?
BASIC interpretation is generally slow, but suitable for certain applications. For reference, "8-Bit Show And Tell" has a library of videos showing how to exploit BASIC to get speed gains.
th-cam.com/users/8BitShowAndTell
Well, it converts integer variables to floating point and back to integers to do integer math, and there is no FPU on a Commodore 64...
The same with DOSBOX and QBASIC vs Debug and x86 assembly.
actually this test does not show anything about basic speed. executing floating point calculations from ML would be exactly as slow as in basic (and btw. this floating point math is coded in ML... oh wait, shouldn't it be very fast then? 🤪)