Nested If constructs

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
User avatar
dynamicdave
Normal user
Posts: 219
Joined: 30 Jan 2017, 20:25
Location: Hampshire, UK

Nested If constructs

#1 Post by dynamicdave » 27 Feb 2020, 09:43

This may seem a silly question.
I've just got this rule-set working to turn an individual LED on depending on the numerical value in an 'event' sent via MQTT (from Node-RED to a Wemos D1 Mini).
I'm surprised it works as I thought that ESP Easy didn't support nested IFs.

I'm running mega-20200222 on the Wemos.

Regards, David

Code: Select all

On ledcolour_code Do
TaskValueSet,1,1,%eventvalue%         // Store %eventvalue% in dummy variable ledcolour#code

if [ledcolour#code]=0                 // Clear all LEDs
    gpio,15,0
    gpio,13,0
    gpio,12,0
else 
    if [ledcolour#code]=1             // Turn on RED LED
        gpio,15,1
        gpio,13,0
        gpio,12,0
    else 
        if [ledcolour#code]=2         // Turn on GREEN LED
            gpio,15,0
            gpio,13,1
            gpio,12,0
        else 
            if [ledcolour#code]=3     // Turn on BLUE LED
                gpio,15,0
                gpio,13,0
                gpio,12,1
            endif
        endif
    endif
endif

endon

User avatar
grovkillen
Core team member
Posts: 3603
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Nested If constructs

#2 Post by grovkillen » 27 Feb 2020, 09:57

I think it's parsing it just as it would parse this:

Code: Select all

On ledcolour_code Do
TaskValueSet,1,1,%eventvalue%         // Store %eventvalue% in dummy variable ledcolour#code

if [ledcolour#code]=0                 // Clear all LEDs
       gpio,15,0
       gpio,13,0
       gpio,12,0
endif
if [ledcolour#code]=1             // Turn on RED LED
        gpio,15,1
        gpio,13,0
        gpio,12,0
endif
if [ledcolour#code]=2         // Turn on GREEN LED
       gpio,15,0
       gpio,13,1
       gpio,12,0
endif
if [ledcolour#code]=3     // Turn on BLUE LED
       gpio,15,0
       gpio,13,0
       gpio,12,1
endif
endon
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

User avatar
dynamicdave
Normal user
Posts: 219
Joined: 30 Jan 2017, 20:25
Location: Hampshire, UK

Re: Nested If constructs

#3 Post by dynamicdave » 27 Feb 2020, 10:23

Thanks.

Is there any speed/performance issues with the way I've implemented it?

Note:
I did code it your way at the start, then thought I could improve the layout/readability.

Thanks again for your swift response.

dampa
Normal user
Posts: 80
Joined: 19 Jul 2018, 01:48

Re: Nested If constructs

#4 Post by dampa » 27 Feb 2020, 12:33

If they parse the same way, there won't be any difference. If they do parse differently you, in the nested case if the ledcolour#code was 0 you would only evaluate one if statement while in the non nested case all four if will always be evaluated. however, if ledcolour#code = 3 the in both cases all four if statements will be evaluated.

So (if they pares differently) on average the nested if's will evaluate 2 if statements while the non-nested if's will alyays evaluate 4 statements.

If won't make a differenct unless you were pushing thru a lot...a very, very lot of data thru that rule

TD-er
Core team member
Posts: 2768
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Nested If constructs

#5 Post by TD-er » 28 Feb 2020, 16:04

Well there is a slight difference.
"else if" is one level deeper. So you may run into the max. depth in some nested situations.
Not sure what it is, but the parser does have a max. depth.

Also there is a very slight difference in parsing more lines, but I guess having less spaces until the comment slashes is probably more likely to be a speed improvement :)

User avatar
dynamicdave
Normal user
Posts: 219
Joined: 30 Jan 2017, 20:25
Location: Hampshire, UK

Re: Nested If constructs

#6 Post by dynamicdave » 28 Feb 2020, 20:07

I think I read somewhere the nested level depth was three(3).
(Which by coincidence is what my code has.)

So are you saying that I should remove all the comments?

I only put them in the code to make it obvious as to what was happening.

Thanks for your valuable feedback

User avatar
grovkillen
Core team member
Posts: 3603
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Nested If constructs

#7 Post by grovkillen » 28 Feb 2020, 21:50

All rows are being parsed and since the comments are preceded by a lot of blanks it must parse through it all until it finds the dual slashes and realize it's a comment. So if you want to optimized the rules you should avoid those spaces and comments. But in reality the win isn't noticed I reckon.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

TD-er
Core team member
Posts: 2768
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Nested If constructs

#8 Post by TD-er » 29 Feb 2020, 16:42

Each byte is parsed as jimmy noted.
So in theory parsing comments does take time, but I doubt it will be noticeable.
If for whatever reason you may run into issues with saving the rules file (2048 bytes max. and some report practical limit of 1500 or slightly more before the rules may get corrupted at saving)

So the only reason to remove the comments is when you run into rules size issues.

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests