![]() |
Forum Index : Microcontroller and PC projects : MM* constants
Author | Message | ||||
Mark Regular Member ![]() Joined: 26/11/2022 Location: United StatesPosts: 85 |
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 KingdomPosts: 2540 |
good point, mm.screen width/height checking |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3285 |
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 StatesPosts: 85 |
Thanks |
||||
stanleyella![]() Guru ![]() Joined: 25/06/2022 Location: United KingdomPosts: 2540 |
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: AustraliaPosts: 2611 |
> clear Saves about 6µS per loop using MM.* rather than Const.> 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 > 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 KingdomPosts: 4038 |
Following Geoff's post, will be slower. But not much. John |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |