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 : A baffling choice
Author | Message | ||||
PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 858 |
The choice function looks like it should be much more efficient a=12 b=2 timer=0 for i = 1 to 10000 d = choice(b>a,55,20) next print timer/10000 timer=0 for i = 1 to 10000 if b>a then d=55 else d=20 end if next print timer/10000 The manual states: Shame because it just looks/feels better. |
||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2135 |
Perhaps a faster way, after True / False is established is to use a 2 element array. Dim String choose(1) = ("hello","bye") Index 0 or 1 determines the result. I found Select Case to be a bit slow when there are a lot of options and a larger version of the array method was much faster. The job was to to translate the 45 key codes from an IR remote control to those of another device. |
||||
PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 858 |
Hi Phil, Yeah, that's a neat alternative to Select Case I get a kick out of these kind of tips. Thanks. I'm not actually looking for faster execution because the ARMmite-H7 has already exceeded expectations; it's just that the "choice" function showed-up as I was perusing the manual and it struck me as more elegant than IF/THEN/ELSE. I just gained 40usec by shortening variable names so I might just give a few back in order to use the "choice" function, for the sake of prettier code |
||||
thwill Guru Joined: 16/09/2019 Location: United KingdomPosts: 4039 |
If I recall correctly CHOICE was definitely faster on the CMM2. On the PicoMite I seem to recall that for at least one flavour (VGA?) the function that implements it was kept in flash and not RAM and thus slower, but I've just checked the code and it is flagged unconditionally as: void __not_in_flash_func(fun_ternary)(void) ... so that doesn't seem to be the case anymore. Alternatively these are all false memories . Best wishes, Tom Edited 2024-02-28 02:31 by thwill Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 858 |
Hmmm, I'm on the ARMmite H7 which I would assume to be similar to the CMM2. No worries, the H7 is a speed-demon, I just wanna use that function Thanks, Tom. |
||||
PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 858 |
I wonder if it's the function call that's robbing the time: OPTION EXPLICIT OPTION DEFAULT integer dim i dim float mcmd mcmd=40 timer=0 for i = 1 to 1000 if mcmd<0.2 then mcmd=0.2 if mcmd>99.8 then mcmd=99.8 next print timer/1000,mcmd timer=0 for i = 1 to 1000 mcmd=max(mcmd,0.2) mcmd=min(mcmd,99.8) next print timer/1000,mcmd |
||||
LeoNicolas Guru Joined: 07/10/2020 Location: CanadaPosts: 479 |
Usually what I like to do is not optimize prematurely the code. I prefer to have a code that easy to read and maintain, leaving the optimization to the end if necessary, which usually is not. |
||||
damos Regular Member Joined: 15/04/2016 Location: AustraliaPosts: 63 |
This is a move to functional programming which I have been doing over the years without realizing. In C# and JS I pretty much always use x = exp ? val1 : val2 which is the same as choice. I also have been using lamba expressions everywhere without realizing how much I have changed. In JS, function dostuff() {} is replaced by: const stuff = () => { } I really don't write much C# as 90% of my code is actually LINQ, which is all functional. These changes have been happening to many languages. One of the big advantages is concurrent programming. While they seem to all be different ways of doing the same thing, using a function allows the compiler to do clever things rather than being forced to implement things using branches, which creates a lot of uncertainty when they optimize pipelines. |
||||
PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 858 |
I think it depends on your interpretation of "optimize". I like robustness and the great thing about this interpreter is that I can test, test, test, doing all the things that "will never happen" in the real world The "I'll come back and tidy it up later" is not acceptable to me because I have lost my train-of-thought. |
||||
Print this page |