Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:25 26 Nov 2024 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 : A baffling choice

Author Message
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 858
Posted: 09:54am 27 Feb 2024
Copy link to clipboard 
Print this post

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

  Quote  
RUN
0.0095116
0.0092647
>


The manual states:
  Quote  
This function allows you to do simple either or selections much more
efficiently and faster than using IF THEN ELSE ENDIF clauses.
The condition is anything that will resolve to nonzero (true) or zero (false)
The expressions are anything that you could normally assign to a variable or
use in a command.
e.g.
print choice(1, "hello","bye") will print "Hello"
print choice(0, "hello","bye") will print "Bye"
a=1:b=1:print choice(a=b, "hello","bye") will print "Hello"


Shame because it just looks/feels better.
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 12:08pm 27 Feb 2024
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 858
Posted: 12:58pm 27 Feb 2024
Copy link to clipboard 
Print this post

  phil99 said  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.


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 Kingdom
Posts: 4039
Posted: 01:38pm 27 Feb 2024
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 858
Posted: 04:28pm 27 Feb 2024
Copy link to clipboard 
Print this post

  thwill said  If I recall correctly CHOICE was definitely faster on the CMM2.


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 Kingdom
Posts: 858
Posted: 05:19pm 27 Feb 2024
Copy link to clipboard 
Print this post

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

  Quote  
0.015143       40
0.016619       40
>
 
LeoNicolas

Guru

Joined: 07/10/2020
Location: Canada
Posts: 479
Posted: 10:12pm 27 Feb 2024
Copy link to clipboard 
Print this post

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: Australia
Posts: 63
Posted: 12:43am 28 Feb 2024
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 858
Posted: 07:01am 28 Feb 2024
Copy link to clipboard 
Print this post

  LeoNicolas said  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.


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


To reply to this topic, you need to log in.

© JAQ Software 2024