Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 21:10 12 Jul 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 : MM* constants

Author Message
Mark
Regular Member

Joined: 26/11/2022
Location: United States
Posts: 85
Posted: 03:42pm 27 Dec 2024
Copy link to clipboard 
Print this post

If a program accesses a MM.* (i.e. MM.FONTWIDTH) or MM.Info(*) constant many times, is execution faster if the value is stored in a program constant (i.e. const fw% = MM.FONTWIDTH) and access that constant.

Also, is accessing a constant any faster than a variable?
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2540
Posted: 05:22pm 27 Dec 2024
Copy link to clipboard 
Print this post

good point, mm.screen width/height checking
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3285
Posted: 08:21pm 27 Dec 2024
Copy link to clipboard 
Print this post

Read only variables (ie, MM.HRES) are generally implemented the same as a built in function (ie, COS()).  This means that it is converted to a one or two byte token which the interpreter uses to index into a table of addresses for each function.  This is very efficient and will return the value with a minimum of CPU cycles.

Constants on the other hand are implemented as ordinary variables except that they have a read only flag set.  Getting the value of one of these involves either looking up a hashed table or searching through a list of values, and that takes many CPU cycles.

So, read only variables are more efficient BUT you should not be over concerned with trying to make the interpreter more efficient.  Your choice of the program logic and algorithms will have a far greater impact on the speed of your program.

Geoff
Edited 2024-12-28 06:24 by Geoffg
Geoff Graham - http://geoffg.net
 
Mark
Regular Member

Joined: 26/11/2022
Location: United States
Posts: 85
Posted: 09:28pm 27 Dec 2024
Copy link to clipboard 
Print this post

Thanks
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2540
Posted: 09:35pm 27 Dec 2024
Copy link to clipboard 
Print this post

so. if I used v_ht=mm.vres:h_wid=mm.hres then any gains? looping and testing
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2611
Posted: 10:53pm 27 Dec 2024
Copy link to clipboard 
Print this post

> clear
> dim integer n,x,y : dim float t,tt
> t=timer:for n=0 to 999999:x=mm.hres:y=mm.vres:next :tt=timer-t:? tt
49708.761
> const h=mm.hres, v=mm.vres
> t=timer:for n=0 to 999999:x=h:y=v:next :tt=timer-t:? tt
55729.417
>
Saves about 6µS per loop using MM.* rather than Const.

Therefore follow Geoff's advice.

Edit.
That was Pico1 at 126MHz, at 378MHz the saving is just 2µS per loop.
Edited 2024-12-28 09:02 by phil99
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4038
Posted: 11:03pm 27 Dec 2024
Copy link to clipboard 
Print this post

  stanleyella said  so. if I used v_ht=mm.vres:h_wid=mm.hres then any gains? looping and testing

Following Geoff's post, will be slower.  But not much.

John
 
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