c++ moment
Result:
if-else = 419813
ternary = 587712
code:
unsigned long timer1, timer2;
volatile uint32_t x;
timer1 = micros();
for (uint32_t iterator = 0; iterator < 10000000; iterator++)
{
if (iterator & 1)
{
x = 1;
}
else
{
x = 2;
}
}
timer1 = micros() - timer1;
timer2 = micros();
for (uint32_t iterator = 0; iterator < 10000000; iterator++)
{
x = (iterator & 1) ? 1 : 2;
}
timer2 = micros() - timer2;
LOG("if-else = ");
LOGLN(timer1);
LOG("ternary = ");
LOGLN(timer2);