See also: part 1, part 2, part 3, part 4 and part 5.
The prolific Jim Gerrie recently posted a video of an MC-10 bouncing ball demo he wrote in BASIC:
Thanks to Jim, the MC-10 joins a long list of 80’s home computers that thought bouncing a ball around the screen was a good demo.
Seeing his demo inspired me to revisit my Benchmarking BASIC series and see if we can expand on this.
I begin with the excellent (and free) Xroar Emulator. For those that want to play along, there is now a web browser version you can try without installing anything, or you can try the original JS Mocha emulator. Both should be more than enough for this experiment.
First, I load up my BENCH.BAS framework so I can do some speed tests. It looks like this:
0 REM BENCH.BAS 5 DIM TE,TM,B,A,TT 10 FORA=0TO3:TIMER=0:TM=TIMER 20 FORB=0TO1000 70 NEXT 80 TE=TIMER-TM:PRINTA,TE 90 TT=TT+TE:NEXT:PRINTTT/A:END
The above sample code will run four tests (0 to 3) and each time it will do “something” 1001 times (0 to 1000) and report the results in CoCo TIMER values (where each number represents 1/60th of a second, give or take). The benchmark code would go between lines 20 and 70.
Let’s see how big of an object we want to display. Using Jim’s video as a reference (where he shows some of the BASIC source code), it looks like 8 characters wide by 9 characters tall:
Based on him using substrings going up to 6, it looks like he may have 7 frames of animation (0-6). Impressive. But let’s just start with displaying 8×9 characters in various ways to see how fast we can do it.
The fastest way in BASIC is to use a PRINT@ statement, so my first test is to see how long it takes to print these characters 1001 times. I will hard-code the location for each line starting with 0 (the top left of the screen). It looks like this:
30 PRINT@0,"12345678";: PRINT@32,"12345678";: PRINT@64,"12345678";: PRINT@96,"12345678";: PRINT@128,"12345678";: PRINT@160,"12345678";: PRINT@192,"12345678";: PRINT@224,"12345678";: PRINT@256,"12345678";
Running that in the benchmark program results in 3986.
Using decimal values is slow, so next I changed them to hex:
30 PRINT@&H0,"12345678";: PRINT@&H20,"12345678";: PRINT@&H40,"12345678";: PRINT@&H60,"12345678";: PRINT@&H80,"12345678";: PRINT@&HA0,"12345678";: PRINT@&HC0,"12345678";: PRINT@&HE0,"12345678";: PRINT@&H100,"12345678";
Parsing a hex value is much faster, so this results in 3018. That’s about 25% faster.
And, using variables is even faster, since no parsing is required. A quick test that sets variables I-Q to each value, then does a PRINT@I through PRINT@Q produces 2940. Not a huge gain, but every little bit helps. That’s probably the fastest one can print something like this in BASIC.
The problem with using multiple variables is you have to update each one every time the object moves. If you just use one variable, and do math for all the other lines, you are doing the same math for each line that would have been done for the variable.
At this point, we need to see which way of doing math is faster. Or, perhaps, maybe we can do it without using math. . .
To be continued…
Had this been on Commoodre, I’d tried using the cursor movement “characters” to draw the whole thing in one go (or rather as many print statements as required due to the max line length limit).
Does the CoCo basic have something similar?
No cursor movement characters, no, but that’s something I made use of in my VIC-20 days.
Pingback: CoCo bouncing BASIC ball, part 2 | Sub-Etha Software
Pingback: CoCo bouncing BASIC ball, part 3 | Sub-Etha Software
Pingback: CoCo bouncing BASIC ball, part 5 | Sub-Etha Software
Pingback: Color BASIC challenge: ball bouncing WITHOUT any IFs? | Sub-Etha Software