mardi 29 août 2017

Reviewing IF conditional code to save CPU cycles

I am reviewing the usage of if condition in my program, in there, i have lines like the following:

if(count > 4) count = 4;

Would it be a good idea to write the above if conditional statement as the following non-branched one?

count = 4*(count> 4) + count*(count<= 4);

I also have the following snippet there:

for (j=0, i=0; j<NCARD_PER_SUIT && i<CARDS_PER_PLAYER+CARDS_ON_BOARD; ++j) {
    if (card_cfg.hearts & cfg_mask[j]) {
        player_hand[i].card.face = j;
        player_hand[i++].card.suit = HEART;
    }
    if (card_cfg.spades & cfg_mask[j]) {
        player_hand[i].card.face = j;
        player_hand[i++].card.suit = SPADE;
    }
    if (card_cfg.clubs & cfg_mask[j]) {
        player_hand[i].card.face = j;
        player_hand[i++].card.suit = CLUB;
    }
    if (card_cfg.diamonds & cfg_mask[j]) {
        player_hand[i].card.face = j;
        player_hand[i++].card.suit = DIAMOND;
    }
}

and wondering if there is good (non-branched) way to right the above, any suggestions?

Aucun commentaire:

Enregistrer un commentaire