Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:05 02 Aug 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 : CMM2 - nested IFs vs. ANDs speed difference

Author Message
jirsoft

Guru

Joined: 18/09/2020
Location: Czech Republic
Posts: 533
Posted: 08:58pm 01 Feb 2021
Copy link to clipboard 
Print this post

Maybe it's widely known (because of amount of BASIC gurus here), but still interesting. I'm usually using nested IFs instead of ANDs because of speed (learned from old slow BASIC variants), so from curiosity I did try to test difference in MMBasic on CMM2:

DIM INTEGER q, i, tim, tim1

'simple ANDs
tim = TIMER
FOR i = 1 TO 1000000
 IF i < 500000 AND (i MOD 2) = 0 AND (i MOD 3) = 0 THEN
   q = 2 * i
 ENDIF
NEXT i

tim = TIMER - tim
?tim

'nested IFs
tim1 = TIMER
FOR i = 1 TO 1000000
 IF i < 500000 THEN
   IF (i MOD 2) = 0 THEN
     IF (i MOD 3) = 0 THEN
       q = 2 * i
     ENDIF
   ENDIF
 ENDIF
NEXT i

tim1 = TIMER - tim1
?tim1
?tim1/tim


Variant with nested IFs is ca 22% faster than ANDs... Maybe is MMBasic evaluating complete AND condition (and not break on first false like some compilers do) or maybe something else, but still interesting fact for speed optimising.
Jiri
Napoleon Commander and SimplEd for CMM2 (GitHub),  CMM2.fun
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 09:57pm 01 Feb 2021
Copy link to clipboard 
Print this post

I wrote a wiki on tweaking code for performance etc. as a sort of open discussion and this is mentioned in there. It can make quite a difference.

There are others there too all with varying degrees of efficacy.

I don't use CMM2 at all - the nearest I get is with H7 "micromites" and at these speeds there is little to cry about in speeds of interpreted languages. Where it pays dividends for me is with the smaller MM2 which maxes out at 48MHz - still fairly respectable but I have found areas where i needed to squeeze every ounce of juice from them and it resulted in a few tricks. On application has dozens of lighting controllers in a wireless star network controlled by a PC. They all have to listen to the network, decrypt an RC4 packet, do some work and reply with an encrypted packet all in 1.5 seconds. It's a walk in the park for a PC or even a MMXtreme but the lowly '170 needs all the help it can get. You can peruse and add to that article here. http://www.fruitoftheshed.com/MMBasic.Some-Hints-Tips-for-writing-efficient-code.ashx  criticism is welcome.
Edited 2021-02-02 08:01 by CaptainBoing
 
jirsoft

Guru

Joined: 18/09/2020
Location: Czech Republic
Posts: 533
Posted: 10:21pm 01 Feb 2021
Copy link to clipboard 
Print this post

Thanks for pointing to interesting article! I'm just not sure, if comments in MMBasic make program slower, I think they are removed during preprocessing...
I saved it as bookmark, when I find something interesting, I will first look there    and maybe later extend it.
Jiri
Napoleon Commander and SimplEd for CMM2 (GitHub),  CMM2.fun
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 10:58pm 01 Feb 2021
Copy link to clipboard 
Print this post

@jirsoft

As you say comments on the CMM2 are preprocessed out, but my understanding is that isn't the case for other MMBasic platforms; certainly doesn't happen on the CMM1.

From an earlier thread (sorry no link) BASICs unfortunately do not traditionally do short-circuit evaluation of boolean expressions. i.e. they evaluate the RHS of an AND even if the LHS has evaluated to false.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 11:00pm 01 Feb 2021
Copy link to clipboard 
Print this post

  jirsoft said  Thanks for pointing to interesting article! I'm just not sure, if comments in MMBasic make program slower, I think they are removed during preprocessing...

ON the CMM2, comments are removed and some spaces also removed during loading so there is no problem there.
The other micromites don't have the benefit of a file system so the code is loaded directly into memory. This is where removing comments etc before loading does help.

On the CMM2, profiling can really help find the fastest way to do things.

Jim
VK7JH
MMedit
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 08:27am 02 Feb 2021
Copy link to clipboard 
Print this post

  jirsoft said  
and maybe later extend it.


please do!
 
William Leue
Guru

Joined: 03/07/2020
Location: United States
Posts: 405
Posted: 04:54pm 03 Feb 2021
Copy link to clipboard 
Print this post

I got tripped up several times by MMBasic not short-circuiting a boolean AND where the first argument evaluates to false. It's ugly where you want to guard against an array index overrun. In Java, C++, Python, etc, one can write:

if (index >= 0 && array[index] = something) then...

but this fails badly in the MMBasic equivalent when index < 0.

I have since learned to be careful about this.

-Bill
 
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