Subroutine placement in BASIC for speed - I'm amazed!

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
Jodo
Drutt
Posts: 25
Joined: Wed Apr 11, 2018 10:52 am
Location: West Yorkshire, UK

Subroutine placement in BASIC for speed - I'm amazed!

Post by Jodo »

I've often read on various threads about placing subroutine's (primarily input and main loop) close to the start of a large program to improve speed and have always thought "well how much difference can it make really?"

Well, I've just got my answer in the real world and I question this no more!

I have a 27 line display subroutine which if placed at the end of a 520 line program takes 19 seconds to complete - if I place it at the start of the program it takes just 9 seconds to complete! I'm amazed at the difference! I feel a major rewrite coming on just as an excercise to see how fast I can get the game (it will be compiled anyway eventually).

Just thought this post might be useful to someone like myself who has never written a BASIC game before.
User avatar
R-Tape
Site Admin
Posts: 6408
Joined: Thu Nov 09, 2017 11:46 am

Re: Subroutine placement in BASIC for speed - I'm amazed!

Post by R-Tape »

Jodo wrote: Sat Mar 02, 2019 10:28 pm I've often read on various threads about placing subroutine's (primarily input and main loop) close to the start of a large program to improve speed and have always thought "well how much difference can it make really?"
Wow. Way more than I would have thought. That's the kind of saving I'd expect from a RANDOMIZE USR routine. I've wondered this kind of thing before, but never tested it.

For optimised pure BASIC games, Diamond Digger is surprisingly fast for the amount that is happening (granted—it really needs to run at 7MHz). Jim must have used every trick in the book to make it close to playable.
I feel a major rewrite coming on just as an excercise to see how fast I can get the game (it will be compiled anyway eventually).
I was going to joke: then you'll need this thread, but I see you started it!
Jodo
Drutt
Posts: 25
Joined: Wed Apr 11, 2018 10:52 am
Location: West Yorkshire, UK

Re: Subroutine placement in BASIC for speed - I'm amazed!

Post by Jodo »

After doing some more testing I'm getting some fluctuating times, I think that my host netbook and the emulator may have a hand in that. Still definitely worth the rewrite though.
R-Tape wrote: Sat Mar 02, 2019 10:48 pm I was going to joke: then you'll need this thread, but I see you started it!
Haha, yes, I'm still a way off that though and now I'm struggling with ram size. I'm hoping that when I finish and get rid of all the REM statements, lose the obsolete code remnants and rename all the memorable variable names to single letters, etc. I'll be able to fit it in the compiler, otherwise I'll have to start thinking about cutting features. The code could probably be massively optimized but unfortunately my brain isn't capable of the mind bending BASIC I often see posted.
I can't get the 128k version of the compiler to work so that isn't an option.
I'm enjoying it all immensely though!
Post Reply