6 ms·
Depending on the machine, a modulo can cost _alot_ . Last time I checked, the operation cost was 26k cycles on my PIC. Using a 2^n + mask made my queue perfor
by fla 10y ago
Depending on the machine, a modulo can cost _alot_ .
Last time I checked, the operation cost was 26k cycles on my PIC.
Using a 2^n + mask made my queue perform 10 times faster (if not more).
- jstimpfle 10y agoDon't use a modulo then. Use subtraction.
- dllthomas 10y agoSubtraction requires a branch, which could be worse (or not) depending on architecture.
- pklausler 10y agoOh, come on, you don't need a branch to do a conditional subtraction. Reify the condition to 0/1 and use multiplication, or use AND with a two's complement of the condition.
- dllthomas 10y agoGood point!
- avocad 10y agoThanks! This AND trick is great. Actually makes me want to go back to assembly-level programming.
- cksk 10y agoOK, my bit twiddling knowledge is weak, my google skills are weaker still, and now I'm curious: what does "AND with a two's complement of the condition" mean, exactly?
- jnordwick 10y agoThe branch will be properly predicted every time except for when it wraps. This should be faster than any of the alternatives.
- dllthomas 10y agoI fully believe that there are plenty of contexts where that's true - particularly in any throughput oriented system where the buffer is large. But if you care, measure.
- deleted 10y ago[deleted]