Execution speed, faster at beginning versus end of long program

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
stroebeljc
Posts: 67
Joined: Thu Apr 23, 2020 6:02 am

Execution speed, faster at beginning versus end of long program

Post by stroebeljc »

I have noticed a significant difference in the execution speed of BASIC lines that are at the beginning of a program versus those at the end of a large program. I do not have a "timing" analysis, but I do notice significant impacts of speed when looping to produce speech sounds when the loops are in these different locations.

I believe it is due to the way line addresses are determined during run-time. The LINE-ADDR ROM routine always starts at the beginning of the program space and checks each line until it gets to the one it wants. Obviously, this can take a lot longer if those lines are at the tail end of a large program.

Has anyone ever noticed this? Is there a discussion about this on this forum that I missed?
John
dr beep
Posts: 2076
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Execution speed, faster at beginning versus end of long program

Post by dr beep »

Yes this is known issue.

Your games best have initialize at then end (only called once) and on a ZX Spectrum DEF FN is better place at the start (scanned first)
Often used code is best placed high as possible in the listings.

Machinecode will just execute what is at the Program Counter and always points there. No problem in MC.
User avatar
1024MAK
Posts: 5118
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Execution speed, faster at beginning versus end of long program

Post by 1024MAK »

Yes, this has been known about since the 1980s and as dr beep says, affects both the ZX81 and the ZX Spectrum.

Hence your first line should be:
10 GOTO 8000 : REM Jump to set-up - note not needed if the program auto-runs to a different line number after loading
20 REM any timing critical code
...

200 REM main loop
...

1000 REM less often used code
...

8000 REM set-up code

9000 REM on ZX Spectrum, any data
9010 DATA
...

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
Post Reply