jeudi 13 juin 2019

Efficiency difference between an if-statement and mod(SIZE)

Studying I found the use of the (i+1)mod(SIZE) to perform a cycle in an array of elements. So I wondered if this method was more efficient than an if-statement...


For example:

#define SIZE 15

int main(int argc, char *argv[]) {
    int items[SIZE];

    for(int i = 0; item[0] < 5; i = (i + 1) % SIZE) items[i] += 1;

    return 0;
}

It is more efficient than(?):

#define SIZE 15

int main(int argc, char *argv[]) {
    int items[SIZE];

    for(int i = 0; item[0] < 5; i++) {
        if(i == SIZE) i = 0;
        items[i] += 1;
    }

    return 0;
}


Thanks for the answers and your time.

Aucun commentaire:

Enregistrer un commentaire