Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 19:17 25 Nov 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 : MMBasic subroutine performance tips

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10646
Posted: 09:58am 23 Nov 2025
Copy link to clipboard 
Print this post

To optimise performance do not use the same name for local variables in nested subroutines.
Variables are created in a hash table. So by definition if you use i as an index in a subroutine called by a subroutine that also uses i. There will have been a hash table collision that the firmware resolves by looking for the next free slot. This is completely benign but does take extra time: not a lot but some.

Also, it is very slightly quicker if variables are typed at declaration rather than being typed with a suffix

Demo 1
fred
'
Sub fred
 Local i%=22,ii%=23
 bert
 Print i%,ii%
End Sub
'
Sub bert
 Local i%,j%,ii%,jj%
 Timer =0
 For i%=1 To 100000
 ii%=i%
 Next
 Print Timer
 Timer =0
 For j%=1 To 100000
 jj%=j%
 Next
Print Timer
End Sub

Demo2
Option explicit
Option default integer
fred
'
Sub fred
 Local i=22,ii=23
 bert
 Print i,ii
End Sub
'
Sub bert
 Local i,j,ii,jj
 Timer =0
 For i=1 To 100000
 ii=i
 Next
 Print Timer
 Timer =0
 For j=1 To 100000
 jj=j
 Next
Print Timer
End Sub
 
LeoNicolas

Guru

Joined: 07/10/2020
Location: Canada
Posts: 542
Posted: 10:37am 23 Nov 2025
Copy link to clipboard 
Print this post

Thank you for the tip

That makes sense. Hash tables have an O(1) time access for the best scenario, and O(N) for the worst case, when keys collide. It is still possible to have a key collision even with keys with different names, if both strings have the same hash, but the chance to happen is very low.

Is it possible to store the local variable in the hash table using a composite key consisting of the method name + variable name? This should solve the issue.
 
Bleep
Guru

Joined: 09/01/2022
Location: United Kingdom
Posts: 693
Posted: 10:26am 24 Nov 2025
Copy link to clipboard 
Print this post

Hi Peter,
While I agree with what you have said, there are exceptions/problems.
I was optimising
Sub mm.user_bitmap(x%,y%,w%,h%,s%,Fc%,Bc%,bitmap%)
to
Sub mm.user_bitmap(x As Integer,y As Integer,w As Integer,h As Integer, s As Integer,Fc As Integer,Bc As Integer,bitmap As Integer)

and removing all the % in the subroutine, but it actually got slower, presumably because the parser had to parse all those 'As Integer' statements, I got round it by using 'Option Default Integer' but this can't work if there are a mixture of types. Assuming it is the length of the declaration, which slows it down, is a short form possible, relativly easy for you? maybe:-

xyz As %, pqr As %, abc As !, ghi As $
Or even
xyz %, pqy %, abc !, ghi $    'note space

This is of course assuming it is the length of the decleration that is causing the slow down, it may not be.?
Regards Kevin.
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1649
Posted: 10:48am 24 Nov 2025
Copy link to clipboard 
Print this post

MM.REPLACE: I haven't seen any mention of it. Could this be used to replace CONSTants with literals for improved performance?
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10646
Posted: 12:07pm 24 Nov 2025
Copy link to clipboard 
Print this post

Kevin

You can use OPTION DEFAULT for your primary variable type and then just type anything else with the appropriate suffix. I would never declare a subroutine using the "as" syntax

  Quote  I haven't seen any mention of it. Could this be used to replace CONSTants with literals for improved performance?

No because is then modifies the source. This is how #define works on the CMM2 because it is totally a disk based system and the user never sees the garbled version that actually runs. The PicoMite is like all other variants where the source is de-tokenised for editing/listing/saving and then tokenised into flash for running. If you then edited a file with #defines they would have disappeared and all the literals would be in the source defeating the object
 
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