lundi 19 septembre 2016

Call a function when counter reaches some value without if statement

Lets say I have a function named loop(). In this loop() I increment a counter count.
I have few functions, A(), B(), C(), etc.

I want to call each one of these functions when the counter reaches some value (different for every function). My current code looks like:

static int count = 0;
void loop(){
    if (count == VALUE_ONE)
        A();
    if (count == VALUE_TWO)
        B();
    if (count == VALUE_THREE)
        C();

    count++;
}

VALUE_* are #defines so they are not being changed during the program.

Right now I am using regular if statements to check the counter value. But I want to avoid using the if statement to avoid branch mispredictions.

Is there a better way to do this? Something that will actually avoid branch mispredictions etc?

Aucun commentaire:

Enregistrer un commentaire