Which of these is faster to execute?
IF A+B+C=0 THEN . . .
or
IF A=0 AND B=0 AND C=0 THEN . . .
Which of these is faster to execute?
IF A+B+C=0 THEN . . .
or
IF A=0 AND B=0 AND C=0 THEN . . .
Russ
N0EVC, xWB6ONT, xWN6ONT
"Easy to use" is easy to say.
This is faster
IF A+B+C=0
The generated ASM
ThisCode:ADD?BBW _A, _B, T1 ADD?WBW T1, _C, T1 CMPNE?WCL T1, 000h, L00001
IF A=0 AND B=0 AND C=0 THEN
Generates
Code:CMPEQ?BCB _A, 000h, T1 CMPEQ?BCB _B, 000h, T2 LAND?BBW T1, T2, T2 CMPEQ?BCB _C, 000h, T3 LAND?WBW T2, T3, T3 CMPF?WL T3, L00003
Dave
Always wear safety glasses while programming.
You will have problem with the first one one day or another (put some values in and figure a roll-over), but the second one is bullet proof.
If you want to measure it, you may use the regular suggestions
- Set a pin before, and clear it after the IF-THEN or code block. Measure the delay with a scope or frequency meter
- Same as above, but use an internal Timer and output the data over serail port
- Use MPLAB stopwatch
http://www.picbasic.co.uk/forum/show...33&postcount=9 <-- really handy
http://www.picbasic.co.uk/forum/show...9&postcount=13
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
Hi, RUSS
A+B+C = 0 has many solutions AS ... you use an ADDITION and not a Bitwise AND !!!
253 + 2 + 1 = 0 .... for Bytes !!!
1 + 1 + 0 = 0 ... for Bits !!!
so, It can't be equivalent to the second line ... Too Bad !
Now, let's suppose A, B, C are BITs ...
so
Will be the fastest ... with PBP.`( 17 asm Lines ...)Code:IF A | B | C THEN Z = 1...
is somewhat longer ...Code:IF (NOT A & NOT B & NOT C ) THEN Z = 1
BUT, if A,B,C are inputs ... would be faster to mask the other inputs and test if result is 0
Only 12 asm lines ...Code:IF ( PORTx & %00000111 ) THEN Z = 1
Alain
Last edited by Acetronics2; - 15th February 2009 at 19:46.
************************************************** ***********************
Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
************************************************** ***********************
IF there is the word "Problem" in your question ...
certainly the answer is " RTFM " or " RTFDataSheet " !!!
*****************************************
Thanks to each and all of you. I appreciate especially the warnings about pitfalls in the summation test; I'll remember those.
I failed to provide the limits/boundary conditions. I should have qualified the question and put it this way:
Which of these is faster to execute?
For A, B, C < 2 (and of course not negative!),
IF A+B+C=0 THEN . . .
or
IF A=0 AND B=0 AND C=0 THEN . . .
It looks as if Dave assumed the limits I had in mind.
The reason for my question was that, back in the bad old days, the syntax of conditional statements in FORTRAN IIb (sometimes called "Weather Bureau FORTRAN") was extremely limited, so doing a test like this was easier, if not necessarily faster.
I happened to try it with PBP in an application, and it worked. I just didn't know how economical it was in terms of generated code. Hence my question.
Last edited by RussMartin; - 15th February 2009 at 19:30.
Russ
N0EVC, xWB6ONT, xWN6ONT
"Easy to use" is easy to say.
Bookmarks