![]() |
Forum Index : Microcontroller and PC projects : CMM2 - nested IFs vs. ANDs speed difference
Author | Message | ||||
jirsoft![]() Guru ![]() Joined: 18/09/2020 Location: Czech RepublicPosts: 533 |
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 KingdomPosts: 2170 |
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 RepublicPosts: 533 |
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 ![]() Jiri Napoleon Commander and SimplEd for CMM2 (GitHub), CMM2.fun |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4311 |
@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: AustraliaPosts: 6283 |
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 KingdomPosts: 2170 |
please do! |
||||
William Leue Guru ![]() Joined: 03/07/2020 Location: United StatesPosts: 405 |
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 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |