View Full Version : The Best Solution
Pesticida
- 26th May 2007, 18:01
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
skimask
- 26th May 2007, 18:33
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
mister_e
- 26th May 2007, 18:44
Yup but this should return a syntax or modifier error. Summe_alarm.0[temp]=x should cure the problem
skimask
- 26th May 2007, 18:48
Summe_alarm.0[temp]=x
DOH! I just woke up, that's my excuse and I'm sticking to it.
Pesticida
- 26th May 2007, 20:25
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
mister_e
- 26th May 2007, 20:29
kind of fussy writing stuff i guess.
further reference:
http://www.picbasic.co.uk/forum/showthread.php?t=544
Pesticida
- 26th May 2007, 21:00
Thank you Mister.
Regard Pesti
T.Jackson
- 27th May 2007, 10:32
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.
mister_e
- 27th May 2007, 19:18
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 ;)
<font color="#000000"> @ __CONFIG _XT_OSC & _LVP_OFF
<font color="#000080">DEFINE </font>HSER_RCSTA 90h <font color="#008000">' Enable serial port & 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] <= $5A) <font color="#000080">OR </font>(ML_Value[0] => $82) <font color="#000080">THEN </font>Summe_Alarm.0 = 1
<font color="#000080">IF </font>(ML_Value[1] <= $5A) <font color="#000080">OR </font>(ML_Value[1] => $82) <font color="#000080">THEN </font>Summe_Alarm.1 = 1
<font color="#000080">IF </font>(ML_Value[2] <= $5A) <font color="#000080">OR </font>(ML_Value[2] => $82) <font color="#000080">THEN </font>Summe_Alarm.2 = 1
<font color="#000080">IF </font>(ML_Value[3] <= $5A) <font color="#000080">OR </font>(ML_Value[3] => $82) <font color="#000080">THEN </font>Summe_Alarm.3 = 1
<font color="#000080">IF </font>(ML_Value[4] <= $5A) <font color="#000080">OR </font>(ML_Value[4] => $82) <font color="#000080">THEN </font>Summe_Alarm.4 = 1
<font color="#000080">IF </font>(ML_Value[5] <= $5A) <font color="#000080">OR </font>(ML_Value[5] => $82) <font color="#000080">THEN </font>Summe_Alarm.5 = 1
<font color="#000080">IF </font>(ML_Value[6] <= $5A) <font color="#000080">OR </font>(ML_Value[6] => $82) <font color="#000080">THEN </font>Summe_Alarm.6 = 1
<font color="#000080">IF </font>(ML_Value[7] <= $5A) <font color="#000080">OR </font>(ML_Value[7] => $82) <font color="#000080">THEN </font>Summe_Alarm.7 = 1
<font color="#000080">IF </font>(ML_Value[0] > $5A) <font color="#000080">AND </font>(ML_Value[0] < $82) <font color="#000080">THEN </font>Summe_Alarm.0 = 0
<font color="#000080">IF </font>(ML_Value[1] > $5A) <font color="#000080">AND </font>(ML_Value[1] < $82) <font color="#000080">THEN </font>Summe_Alarm.1 = 0
<font color="#000080">IF </font>(ML_Value[2] > $5A) <font color="#000080">AND </font>(ML_Value[2] < $82) <font color="#000080">THEN </font>Summe_Alarm.2 = 0
<font color="#000080">IF </font>(ML_Value[3] > $5A) <font color="#000080">AND </font>(ML_Value[3] < $82) <font color="#000080">THEN </font>Summe_Alarm.3 = 0
<font color="#000080">IF </font>(ML_Value[4] > $5A) <font color="#000080">AND </font>(ML_Value[4] < $82) <font color="#000080">THEN </font>Summe_Alarm.4 = 0
<font color="#000080">IF </font>(ML_Value[5] > $5A) <font color="#000080">AND </font>(ML_Value[5] < $82) <font color="#000080">THEN </font>Summe_Alarm.5 = 0
<font color="#000080">IF </font>(ML_Value[6] > $5A) <font color="#000080">AND </font>(ML_Value[6] < $82) <font color="#000080">THEN </font>Summe_Alarm.6 = 0
<font color="#000080">IF </font>(ML_Value[7] > $5A) <font color="#000080">AND </font>(ML_Value[7] < $82) <font color="#000080">THEN </font>Summe_Alarm.7 = 0
<font color="#000080">@ SIZEEND "IF-THEN-ELSE"
@ SIZESTART
FOR </font>TEMP = 0 <font color="#000080">TO </font>7
<font color="#000080">IF </font>(ML_Value[TEMP] <= $5A) <font color="#000080">OR </font>(ML_Value[TEMP] => $82) <font color="#000080">THEN </font>Summe_Alarm.0[TEMP] = 1
<font color="#000080">IF </font>(ML_Value[TEMP] > $5A) <font color="#000080">AND </font>(ML_Value[TEMP] < $82) <font color="#000080">THEN </font>Summe_Alarm.0[TEMP] = 0
<font color="#000080">NEXT </font>TEMP
<font color="#000080">@ SIZEEND "FOR-TO-NEXT"
@ SIZESTART
</font>TEMP = 0
<font color="#000080">REPEAT
IF </font>(ML_Value[TEMP] <= $5A) <font color="#000080">OR </font>(ML_Value[TEMP] => $82) <font color="#000080">THEN </font>Summe_Alarm.0[TEMP] = 1
<font color="#000080">IF </font>(ML_Value[TEMP] > $5A) <font color="#000080">AND </font>(ML_Value[TEMP] < $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 "REPEAT - UNTIL"
@ SIZESTART
</font>TEMP = 0
<font color="#000080">WHILE </font>TEMP!=8
<font color="#000080">IF </font>(ML_Value[TEMP] <= $5A) <font color="#000080">OR </font>(ML_Value[TEMP] => $82) <font color="#000080">THEN </font>Summe_Alarm.0[TEMP] = 1
<font color="#000080">IF </font>(ML_Value[TEMP] > $5A) <font color="#000080">AND </font>(ML_Value[TEMP] < $82) <font color="#000080">THEN </font>Summe_Alarm.0[TEMP] = 0
TEMP=TEMP+1
<font color="#000080">WEND
@ SIZEEND "WHILE - WEND"
</font>here: <font color="#000080">GOTO </font>here
DisplayResult:
<font color="#000080">READCODE </font>ADDR, TWOCHAR
CHAR = TWOCHAR >> 7
<font color="#000080">IF </font>CHAR =0 <font color="#000080">THEN </font>EndString
<font color="#000080">HSEROUT</font>[CHAR]
CHAR = TWOCHAR & $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>[": ",<font color="#000080">DEC </font>size," WORDS, ",<font color="#000080">DEC </font>TMR1, " UsEC",13,10]
<font color="#000080">RETURN
</font>
Is it me, or i see some interrogation mark over some head? ;)
mister_e
- 28th May 2007, 00:24
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
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"
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
Darrel Taylor
- 28th May 2007, 00:56
Darrel may recognize some of his tricks
Ha,
I knew that stuff would come in handy some day. :)
Nice comparisons.
<br>
T.Jackson
- 28th May 2007, 01:17
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/>
mister_e
- 28th May 2007, 04:01
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 :D
(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
Embedded Strings in your Code Space (http://www.picbasic.co.uk/forum/showthread.php?t=1999)
While we're on the subject of optimization... (http://www.picbasic.co.uk/forum/showthread.php?t=5445)
The EXT (external) modifier. (http://www.picbasic.co.uk/forum/showthread.php?t=3891)
How much code would a code hog hog (http://www.picbasic.co.uk/forum/showthread.php?t=2418)
instruction execution time (http://www.picbasic.co.uk/forum/showthread.php?t=365)
See what you have done with me !
mister_e
- 28th May 2007, 04:46
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
@ 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
@ 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
:eek: That's 46 uSec BY single instructions/lines like that. So... much revealant when you use them a loop that the loop itself.
paul borgmeier
- 28th May 2007, 04:56
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?
mister_e
- 28th May 2007, 05:00
Worth to measure it... do we need to start a 'coding challenge' here... if so, i'll warn Darrel, he like that :D
paul borgmeier
- 28th May 2007, 05:19
Worth to measure it... do we need to start a 'coding challenge' here... if so, i'll warn Darrel, he like that :D
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
mister_e
- 28th May 2007, 05:25
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 :D
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.