looping until a lock is set
ags
Posts: 386
I think this is just a matter of style and/or personal taste. In the Propeller Manual (and many other places) I see this idiom frequently used to wait until a lock can be obtained:
This is "backwards" from my way of thinking (not wrong, just reverse-logic for me). Am I missing some subtlety of why one wouldn't use:
? I don't think I've ever seen the latter form, always the former, which makes me wonder if I'm missing something.
repeat until not lockset(lockID)
This is "backwards" from my way of thinking (not wrong, just reverse-logic for me). Am I missing some subtlety of why one wouldn't use:
repeat while lockset(lockID)
? I don't think I've ever seen the latter form, always the former, which makes me wonder if I'm missing something.
Comments
...and that's precisely why I favor it in general (I guess I'm a stingy nanosecond spendthrift)
Thanks for the reply, Mike.
Yes, that is the fastest repeat loop available. Unfortunately, I think it fails at least one other design requirement...
Edit: I thought this was a joke. Isn't repeat alone on a line an infinite loop? (if there is no indent on the 2nd line)
That said, a shorter loop time isn't a guarantee for faster overall time. It all comes down to when the lock becomes available in relation to the actual check. I've seen cases where the one liner was actually faster (waiting for a PASM command ACK) despite taking up more space.
Yes, (now that you point it out) of course. I still find myself thinking (and coding) as if "while" can be the first token of a statement. I tend to leave the "repeat" part out. The empty body assisted in my confusion. Same for a repeat/until loop.
Notwithstanding your explanation about byte codes, it's still not obvious (or clear) to me why the former (one line) is slower than the latter (two lines). I'm confident you are correct, but intuition would have led me to expect that the one line format would be faster (the entire line could in theory be handled by one byte code - since there is no place for any interceding statements (the body of the loop)) while the two-line style would result in two separate steps (meaning two byte codes to interpret). I have never poked into the byte codes so clearly my intuition is misinformed...