The Best Solution


Closed Thread
Results 1 to 18 of 18
  1. #1

    Default The Best Solution

    Hi ,

    I Have a question?

    What is the best Solution to simplify this code:

    if (ML_Value[0] <= $5A) or (ML_Value[0] => $82) then Summe_Alarm.0 = 1
    if (ML_Value[1] <= $5A) or (ML_Value[1] => $82) then Summe_Alarm.1 = 1
    if (ML_Value[2] <= $5A) or (ML_Value[2] => $82) then Summe_Alarm.2 = 1
    if (ML_Value[3] <= $5A) or (ML_Value[3] => $82) then Summe_Alarm.3 = 1
    if (ML_Value[4] <= $5A) or (ML_Value[4] => $82) then Summe_Alarm.4 = 1
    if (ML_Value[5] <= $5A) or (ML_Value[5] => $82) then Summe_Alarm.5 = 1
    if (ML_Value[6] <= $5A) or (ML_Value[6] => $82) then Summe_Alarm.6 = 1
    if (ML_Value[7] <= $5A) or (ML_Value[7] => $82) then Summe_Alarm.7 = 1

    if (ML_Value[0] > $5A) and (ML_Value[0] < $82) then Summe_Alarm.0 = 0
    if (ML_Value[1] > $5A) and (ML_Value[1] < $82) then Summe_Alarm.1 = 0
    if (ML_Value[2] > $5A) and (ML_Value[2] < $82) then Summe_Alarm.2 = 0
    if (ML_Value[3] > $5A) and (ML_Value[3] < $82) then Summe_Alarm.3 = 0
    if (ML_Value[4] > $5A) and (ML_Value[4] < $82) then Summe_Alarm.4 = 0
    if (ML_Value[5] > $5A) And (ML_Value[5] < $82) then Summe_Alarm.5 = 0
    if (ML_Value[6] > $5A) and (ML_Value[6] < $82) then Summe_Alarm.6 = 0
    if (ML_Value[7] > $5A) and (ML_Value[7] < $82) then Summe_Alarm.7 = 0

    Thanks a lot for any answer.

    Regard Pesti
    Last edited by Pesticida; - 26th May 2007 at 18:19.

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Code:
    for temp = 0 to 7
    if (ML_Value[temp] <= $5A) or (ML_Value[temp] => $82) then Summe_Alarm.temp = 1
    if (ML_Value[temp] > $5A) and (ML_Value[temp] < $82) then Summe_Alarm.temp = 0
    next temp

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Yup but this should return a syntax or modifier error. Summe_alarm.0[temp]=x should cure the problem
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    Summe_alarm.0[temp]=x
    DOH! I just woke up, that's my excuse and I'm sticking to it.

  5. #5


    Did you find this post helpful? Yes | No

    Default

    Thank you for the very good solution !
    Another Question :-)
    Why I need Zero for : Summe_Alarm.0[Temp] and why not just
    Summe_Alarm.[Temp] ?
    I know that I receive a syntax but I dont understand what is the reason for use Zero !

    Regard Pesti
    Last edited by Pesticida; - 26th May 2007 at 21:00.

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    kind of fussy writing stuff i guess.

    further reference:
    http://www.picbasic.co.uk/forum/showthread.php?t=544
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7


    Did you find this post helpful? Yes | No

    Default

    Thank you Mister.

    Regard Pesti

  8. #8
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Post

    I think your original code would work marginally faster. However, it would also consume much more code space. So that's the compromise I guess. Out of interest, (DO's & Whiles) are significantly much faster than (For Nexts) in Visual Basic.

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    This has picked my curiosity, so here's a little benchmark test
    IF-THEN-ELSE: 432 WORDS, 1608 UsEC
    FOR-TO-NEXT: 103 WORDS, 2241 UsEC
    REPEAT - UNTIL: 99 WORDS, 2200 UsEC
    WHILE - WEND: 101 WORDS, 2225 UsEC
    Interesting eh!

    and the code to evaluate it, Darrel may recognize some of his tricks
    Code:
    <font color="#000000">        @   __CONFIG _XT_OSC &amp; _LVP_OFF
    
        <font color="#000080">DEFINE </font>HSER_RCSTA 90h <font color="#008000">' Enable serial port &amp; continuous receive
        </font><font color="#000080">DEFINE </font>HSER_TXSTA 24h <font color="#008000">' Enable transmit, BRGH = 1
        </font><font color="#000080">DEFINE </font>HSER_SPBRG 25  <font color="#008000">' 9600 Baud @ 4MHz, 0.16%
    
        </font>@TMR1=TMR1L
        TMR1    <font color="#000080">VAR WORD </font>EXT
        T1CON = 0
    
        ML_Value        <font color="#000080">VAR BYTE</font>[8]
        Summe_Alarm     <font color="#000080">VAR BYTE
        </font>TEMP            <font color="#000080">VAR BYTE
        </font>ADDR            <font color="#000080">VAR WORD
        </font>TWOCHAR         <font color="#000080">VAR WORD
        </font>CHAR            <font color="#000080">VAR BYTE
        </font>size <font color="#000080">VAR WORD
    
        GOTO </font>START        
    
    <font color="#000080">ASM
    SIZESTART macro
        MOVE?CT 0,T1CON,TMR1ON              </font><font color="#008000">; Stop timer
        </font><font color="#000080">MOVE?CW 0,TMR1                      </font><font color="#008000">; clear Timer value
    </font><font color="#000080">BLOCKSTART = $                          </font><font color="#008000">; set CodeBlock Start address
        </font><font color="#000080">MOVE?CT 1,T1CON,TMR1ON              </font><font color="#008000">; start Timer
        </font><font color="#000080">ENDM
    
    SIZEEND MACRO BLOCKNAME
        MOVE?CT 0, T1CON,TMR1ON             </font><font color="#008000">; stop timer
        </font><font color="#000080">MOVE?CW ($ - BLOCKSTART - 2), _size </font><font color="#008000">; calculate code block size
                                            ; remove 2 word for timerstart/stop
        </font><font color="#000080">LOCAL STRING,JUMPSTRING
        GOTO JUMPSTRING
    STRING    DA  BLOCKNAME,0               </font><font color="#008000">; store codeblock name
    </font><font color="#000080">JUMPSTRING
        CHK?RP _ADDR
        MOVE?CW STRING,_ADDR                </font><font color="#008000">; get string sddress
        </font><font color="#000080">L?CALL _DisplayResult               </font><font color="#008000">; show results via serial comm
        </font><font color="#000080">endm
    ENDASM
    
    </font>START:
    <font color="#000080">@    SIZESTART
        IF </font>(ML_Value[0] &lt;= $5A) <font color="#000080">OR </font>(ML_Value[0] =&gt; $82) <font color="#000080">THEN </font>Summe_Alarm.0 = 1
        <font color="#000080">IF </font>(ML_Value[1] &lt;= $5A) <font color="#000080">OR </font>(ML_Value[1] =&gt; $82) <font color="#000080">THEN </font>Summe_Alarm.1 = 1
        <font color="#000080">IF </font>(ML_Value[2] &lt;= $5A) <font color="#000080">OR </font>(ML_Value[2] =&gt; $82) <font color="#000080">THEN </font>Summe_Alarm.2 = 1
        <font color="#000080">IF </font>(ML_Value[3] &lt;= $5A) <font color="#000080">OR </font>(ML_Value[3] =&gt; $82) <font color="#000080">THEN </font>Summe_Alarm.3 = 1
        <font color="#000080">IF </font>(ML_Value[4] &lt;= $5A) <font color="#000080">OR </font>(ML_Value[4] =&gt; $82) <font color="#000080">THEN </font>Summe_Alarm.4 = 1
        <font color="#000080">IF </font>(ML_Value[5] &lt;= $5A) <font color="#000080">OR </font>(ML_Value[5] =&gt; $82) <font color="#000080">THEN </font>Summe_Alarm.5 = 1
        <font color="#000080">IF </font>(ML_Value[6] &lt;= $5A) <font color="#000080">OR </font>(ML_Value[6] =&gt; $82) <font color="#000080">THEN </font>Summe_Alarm.6 = 1
        <font color="#000080">IF </font>(ML_Value[7] &lt;= $5A) <font color="#000080">OR </font>(ML_Value[7] =&gt; $82) <font color="#000080">THEN </font>Summe_Alarm.7 = 1
        
        <font color="#000080">IF </font>(ML_Value[0] &gt; $5A) <font color="#000080">AND </font>(ML_Value[0] &lt; $82) <font color="#000080">THEN </font>Summe_Alarm.0 = 0
        <font color="#000080">IF </font>(ML_Value[1] &gt; $5A) <font color="#000080">AND </font>(ML_Value[1] &lt; $82) <font color="#000080">THEN </font>Summe_Alarm.1 = 0
        <font color="#000080">IF </font>(ML_Value[2] &gt; $5A) <font color="#000080">AND </font>(ML_Value[2] &lt; $82) <font color="#000080">THEN </font>Summe_Alarm.2 = 0
        <font color="#000080">IF </font>(ML_Value[3] &gt; $5A) <font color="#000080">AND </font>(ML_Value[3] &lt; $82) <font color="#000080">THEN </font>Summe_Alarm.3 = 0
        <font color="#000080">IF </font>(ML_Value[4] &gt; $5A) <font color="#000080">AND </font>(ML_Value[4] &lt; $82) <font color="#000080">THEN </font>Summe_Alarm.4 = 0
        <font color="#000080">IF </font>(ML_Value[5] &gt; $5A) <font color="#000080">AND </font>(ML_Value[5] &lt; $82) <font color="#000080">THEN </font>Summe_Alarm.5 = 0
        <font color="#000080">IF </font>(ML_Value[6] &gt; $5A) <font color="#000080">AND </font>(ML_Value[6] &lt; $82) <font color="#000080">THEN </font>Summe_Alarm.6 = 0
        <font color="#000080">IF </font>(ML_Value[7] &gt; $5A) <font color="#000080">AND </font>(ML_Value[7] &lt; $82) <font color="#000080">THEN </font>Summe_Alarm.7 = 0
    <font color="#000080">@   SIZEEND &quot;IF-THEN-ELSE&quot;
    
    @   SIZESTART
        FOR </font>TEMP = 0 <font color="#000080">TO </font>7
            <font color="#000080">IF </font>(ML_Value[TEMP] &lt;= $5A) <font color="#000080">OR </font>(ML_Value[TEMP] =&gt; $82) <font color="#000080">THEN </font>Summe_Alarm.0[TEMP] = 1
            <font color="#000080">IF </font>(ML_Value[TEMP] &gt; $5A) <font color="#000080">AND </font>(ML_Value[TEMP] &lt; $82) <font color="#000080">THEN </font>Summe_Alarm.0[TEMP] = 0
            <font color="#000080">NEXT </font>TEMP
    <font color="#000080">@   SIZEEND &quot;FOR-TO-NEXT&quot;
     
    @   SIZESTART
        </font>TEMP = 0
        <font color="#000080">REPEAT
            IF </font>(ML_Value[TEMP] &lt;= $5A) <font color="#000080">OR </font>(ML_Value[TEMP] =&gt; $82) <font color="#000080">THEN </font>Summe_Alarm.0[TEMP] = 1
            <font color="#000080">IF </font>(ML_Value[TEMP] &gt; $5A) <font color="#000080">AND </font>(ML_Value[TEMP] &lt; $82) <font color="#000080">THEN </font>Summe_Alarm.0[TEMP] = 0
            TEMP=TEMP+1
            <font color="#000080">UNTIL </font>TEMP = 8
    <font color="#000080">@  SIZEEND &quot;REPEAT - UNTIL&quot;
    
    @   SIZESTART
        </font>TEMP = 0
        <font color="#000080">WHILE </font>TEMP!=8
            <font color="#000080">IF </font>(ML_Value[TEMP] &lt;= $5A) <font color="#000080">OR </font>(ML_Value[TEMP] =&gt; $82) <font color="#000080">THEN </font>Summe_Alarm.0[TEMP] = 1
            <font color="#000080">IF </font>(ML_Value[TEMP] &gt; $5A) <font color="#000080">AND </font>(ML_Value[TEMP] &lt; $82) <font color="#000080">THEN </font>Summe_Alarm.0[TEMP] = 0
            TEMP=TEMP+1
            <font color="#000080">WEND
    @   SIZEEND &quot;WHILE - WEND&quot;
    
    </font>here: <font color="#000080">GOTO </font>here
    
    DisplayResult:
        <font color="#000080">READCODE </font>ADDR, TWOCHAR
        CHAR = TWOCHAR &gt;&gt; 7
            <font color="#000080">IF </font>CHAR =0 <font color="#000080">THEN </font>EndString
            <font color="#000080">HSEROUT</font>[CHAR]
            
        CHAR = TWOCHAR &amp; $7F
            <font color="#000080">IF </font>CHAR = 0 <font color="#000080">THEN </font>EndString
            <font color="#000080">HSEROUT </font>[CHAR]
            
        ADDR=ADDR+1
        <font color="#000080">GOTO </font>DisplayResult
    
        EndString:
        <font color="#000080">HSEROUT </font>[&quot;: &quot;,<font color="#000080">DEC </font>size,&quot; WORDS, &quot;,<font color="#000080">DEC </font>TMR1, &quot; UsEC&quot;,13,10]
        
        <font color="#000080">RETURN           
    
    </font>
    Is it me, or i see some interrogation mark over some head?
    Last edited by mister_e; - 28th May 2007 at 00:00.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    deuhhh, if my Sunday's brain and logic is still good... the conditions could be reduced a little bit, but multiple IF-THENs still execute faster
    Code:
    START:
    @   SIZESTART
        if (ML_Value[0] > $5A) and (ML_Value[0] < $82) then 
            Summe_Alarm.0 = 0
            else
                summe_alarm.0 = 1
            endif
    
        if (ML_Value[1] > $5A) and (ML_Value[1] < $82) then 
            Summe_Alarm.1 = 0
            else
                summe_alarm.1 = 1
            endif
        
        if (ML_Value[2] > $5A) and (ML_Value[2] < $82) then 
            Summe_Alarm.2 = 0
            else
                summe_alarm.2 = 1
            endif
    
        if (ML_Value[3] > $5A) and (ML_Value[3] < $82) then 
            Summe_Alarm.3 = 0
            else
                summe_alarm.3 = 1
            endif
    
        if (ML_Value[4] > $5A) and (ML_Value[4] < $82) then 
            Summe_Alarm.4 = 0
            else
                summe_alarm.4 = 1
            endif
    
        if (ML_Value[5] > $5A) And (ML_Value[5] < $82) then 
            Summe_Alarm.5 = 0
            else
                summe_alarm.5 = 1
            endif
    
        if (ML_Value[6] > $5A) and (ML_Value[6] < $82) then 
            Summe_Alarm.6 = 0
            else
                summe_alarm.6 = 1
            endif
    
        if (ML_Value[7] > $5A) and (ML_Value[7] < $82) then 
            Summe_Alarm.7 = 0
            else
                summe_alarm.7 = 1
            endif
    
    @   SIZEEND "IF-THEN-ELSE"
    
    @   SIZESTART
        for temp = 0 to 7
            if (ML_Value[temp] > $5A) and (ML_Value[temp] < $82) then 
                Summe_Alarm.0[temp] = 0
                else
                    Summe_Alarm.0[temp] = 1
                endif    
            next temp
    @   SIZEEND "FOR-TO-NEXT"
     
    @   SIZESTART
        temp = 0
        repeat
            if (ML_Value[temp] > $5A) and (ML_Value[temp] < $82) then 
                Summe_Alarm.0[temp] = 0
                else
                    summe_Alarm.0[temp]=1
                endif
            temp=temp+1
            until temp = 8
    @  SIZEEND "REPEAT - UNTIL"
    
    @   SIZESTART
        temp = 0
        while temp!=8
            if (ML_Value[temp] > $5A) and (ML_Value[temp] < $82) then 
                Summe_Alarm.0[temp] = 0
                else
                    Summe_Alarm.0[temp] = 1
                endif                
            temp=temp+1
            wend
    @   SIZEEND "WHILE - WEND"
    Quote Originally Posted by BenchMark Report

    IF-THEN-ELSE: 248 WORDS, 827 UsEC
    FOR-TO-NEXT: 70 WORDS, 1380 UsEC
    REPEAT - UNTIL: 66 WORDS, 1339 UsEC
    WHILE - WEND: 68 WORDS, 1364 UsEC
    Last edited by mister_e; - 28th May 2007 at 00:53.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Darrel may recognize some of his tricks
    Ha,

    I knew that stuff would come in handy some day.

    Nice comparisons.
    <br>
    DT

  12. #12
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Post

    Thanks for sharing those figures with us Steve. Repeat is the same as a conditional (DO Loop) ? Main reason DO's are faster is because unlike For Nexts, they don't have counters. In VB, (For Nexts) are idiot proof making them relatively bloated & slow.
    <br/>
    Last edited by T.Jackson; - 28th May 2007 at 01:23.

  13. #13
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    Ha,

    I knew that stuff would come in handy some day.
    yup, kinda REALLY handy stuff. I use it a lot here in a custom INCLUDE FILE! YES you heard me, STEVE USE INCLUDE FILES NOW
    (refer to those http://www.picbasic.co.uk/forum/showthread.php?t=2108 and http://www.picbasic.co.uk/forum/showthread.php?t=2400 )

    So it's a kind of personal mix of old and pretty old previous thread

    Those wanting to know where the idea came from, i think those bellow are the main resources
    See what you have done with me !
    Last edited by mister_e; - 28th May 2007 at 04:07.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  14. #14
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by T.Jackson View Post
    Repeat is the same as a conditional (DO Loop) ?
    Yes indeed.

    Main reason DO's are faster is because unlike For Nexts, they don't have counters.
    mmm yes and no. Here what reduce the speed is more likely the .0[Temp] stuff. Let's see

    Code:
    @   SIZESTART
        for temp = 0 to 7
            next temp
    @   SIZEEND "FOR-TO-NEXT"
     
    @   SIZESTART
        temp = 0
        repeat
            temp=temp+1
            until temp = 8
    @  SIZEEND "REPEAT - UNTIL"

    FOR-TO-NEXT: 13 WORDS, 113 UsEC
    REPEAT - UNTIL: 9 WORDS, 72 UsEC
    about the same code space, 41uSec of difference in the speed for the whole thing.

    But now, have a look at this
    Code:
    @   SIZESTART
        Summe_Alarm.0 = 0
    @   SIZEEND "Direct Bit Addressing"
     
    @   SIZESTART
        temp = 0
        summe_Alarm.0[temp] = 0
    @  SIZEEND ".0[Temp] stuff"

    Direct Byte Addressing: 1 WORDS, 1 UsEC
    .0[Temp] stuff: 10 WORDS, 47 UsEC
    That's 46 uSec BY single instructions/lines like that. So... much revealant when you use them a loop that the loop itself.
    Last edited by mister_e; - 28th May 2007 at 04:56.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  15. #15
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Code:
    IF (ml_value[temp]-$5B)<$27 THEN
        Summe_alarm.0[temp]=0
    ELSE
        summe_alarm.0[temp]=1
    ENDIF
    When used with FOR NEXT or any of the others, this should further cut time and code space .....

    ASM anyone?
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  16. #16
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Worth to measure it... do we need to start a 'coding challenge' here... if so, i'll warn Darrel, he like that
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  17. #17
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Quote Originally Posted by mister_e View Post
    Worth to measure it... do we need to start a 'coding challenge' here... if so, i'll warn Darrel, he like that
    I didn't take the time to check everything except code words for a few cases. It looks like it saves some; the amount depends on what it is inserted between.

    you might want to warn Bruce as well
    Last edited by paul borgmeier; - 28th May 2007 at 05:23. Reason: added more detail
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  18. #18
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Yes but first, we have to get rid of the .0[temp] for speed (see previous post).

    i'm thinking of some kind of aliasing... but, i keep thinking...

    Yeah, it's getting interesting
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Strange I2C problem (and solution)
    By brid0030 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th December 2008, 19:14
  2. Replies: 6
    Last Post: - 20th September 2008, 12:28
  3. Replies: 7
    Last Post: - 7th February 2008, 09:46
  4. Need PC software solution
    By mister_e in forum Off Topic
    Replies: 9
    Last Post: - 27th November 2005, 02:18
  5. Battery Solution
    By Keith in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 22nd July 2005, 06:07

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts