![]() |
Forum Index : Microcontroller and PC projects : CMM2 - Slowdown not returning from SUBs
Author | Message | ||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
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 KingdomPosts: 4311 |
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 KingdomPosts: 10315 |
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 KingdomPosts: 4311 |
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: AustraliaPosts: 341 |
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. 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. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |