One thing you could try is to build an "if
-ladder" to only make one write to i
; by rolling the lower bits into each the higher test's value, and reversing the order of the tests.
int i;
if (m > speed3) i = B00001111;
else if(m > speed2) i = B00000111;
else if(m > speed1) i = B00000011;
else if(m > speed0) i = B00000001;
else
i = 0;
This also cuts away the read-modify-write, since it always does a single write to i
. You're still going to have to profile this, I really can't say if it's going to be faster.
Oh, and the thing that looks like a binary prefix (the B
) is an Arduino-ism, not standard in C. I think it's just 256 preprocessor macros.