Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 05:14 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2 - Slowdown not returning from SUBs

Author Message
capsikin
Guru

Joined: 30/06/2020
Location: Australia
Posts: 341
Posted: 04:37am 26 Jul 2020
Copy link to clipboard 
Print this post

When I was looking at optimising boulderdash with abraxis, I noticed that it seemed to get slower if there were some extra sub calls left in the stack (they had several local variables, some which were large arrays, in case that's important).

It wasn't a huge difference, I think maybe 50% speed difference if I had almost enough extra calls on the stack to crash the program.

Does anyone know why this would be? I'm asking in case understanding it can help me with speed optimisation.

The crashing part already makes sense, there must be some limit on how many subs you can have in the call stack.
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 06:31am 26 Jul 2020
Copy link to clipboard 
Print this post

I'm obviously missing something. Why are you not returning from these Subs?

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10315
Posted: 06:33am 26 Jul 2020
Copy link to clipboard 
Print this post

One big overhead in functions and subroutines is local variables as they get created and deleted each time the subroutine is called. Obviously one of the big benefits of subroutines and functions is precisely that they have local variables though  

Setting variables as STATIC may improve this (untested)  but you then need to be careful that you don't assume they are set to zero when used. Local strings and arrays are worst case as they have to get memory off the MMBasic heap as well as allocating the variable header.

Making sure variable names are unique in the first 4 characters will offer minor advantages.

Finally check the new MATH command for simple things like setting arrays, copying arrays and finding MINs and MAXs in arrays. This will operate much faster than for loops in Basic
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 07:16am 26 Jul 2020
Copy link to clipboard 
Print this post

  matherp said  Making sure variable names are unique in the first 4 characters will offer minor advantages.


What about function and subroutine name lookup? I think I convinced myself with Z-MIM that short names help here too?

Regards,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
capsikin
Guru

Joined: 30/06/2020
Location: Australia
Posts: 341
Posted: 08:06am 26 Jul 2020
Copy link to clipboard 
Print this post

  thwill said  I'm obviously missing something. Why are you not returning from these Subs?

Tom


In this case it was a bug. Calling GOTO from a keyboard sub to a location outside the sub. But you might have a top level sub that calls a sequence of other subs, first for setting stuff up, then call a final sub which is the main program loop that continues forever.

  matherp said  One big overhead in functions and subroutines is local variables as they get created and deleted each time the subroutine is called. Obviously one of the big benefits of subroutines and functions is precisely that they have local variables though  

Setting variables as STATIC may improve this (untested)  but you then need to be careful that you don't assume they are set to zero when used. Local strings and arrays are worst case as they have to get memory off the MMBasic heap as well as allocating the variable header.


There were some arrays. I wonder if it could get slower as the variables are having to be added to a longer list or larger heap.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025