0

Doesn't really matter for this specific case but what would be better practice and what would be faster? I think the latter for both as I have read you shouldn't hardcode any numbers bigger than 1 and also id guess that an INC would be faster than an ADD in Assembly. Although I think the former looks better. Or is there a better way?

for (int i = 0; i < CARDS_PER_HAND; i++)
{
    playerHand[i] = getDeck[deckIndex];
    hellmuthHand[i] = getDeck[deckIndex + 1];
    dwanHand[i] = getDeck[deckIndex + 2];
    iveyHand[i] = getDeck[deckIndex + 3];
    negreanuHand[i] = getDeck[deckIndex + 4];
    deckIndex += 5;
}

or

for (int i = 0; i < CARDS_PER_HAND; i++)
{
    playerHand[i] = getDeck[deckIndex];
    deckIndex ++;
    hellmuthHand[i] = getDeck[deckIndex];
    deckIndex++;
    dwanHand[i] = getDeck[deckIndex];
    deckIndex ++;
    iveyHand[i] = getDeck[deckIndex];
    deckIndex ++;
    negreanuHand[i] = getDeck[deckIndex];
    deckIndex ++;
}
NOP da CALL
  • 851
  • 2
  • 8
  • 15
  • First is better. You have less assignment operations. – ABCD Feb 02 '17 at 04:46
  • 3
    Pretty sure any sane optimizer will massage that code to "something optimal-ish". Go for whatever is most readable, or whatever is harder to screw up. – user508633 Feb 02 '17 at 04:47
  • 2
    You could also use `getDeck[deckIndex++]`. The important thing is to use which ever solution you feel is easiest to read and maintain. – p.s.w.g Feb 02 '17 at 04:48
  • 2
    In addition to what @user508633 has said, I also recommend using `playerHand[i] = getDeck[deckIndex++]; hellmuthHand[i] = getDeck[deckIndex++];` and so on, since this would evaluate to `deckIndex` *and* increment it (after evaluation, and that's why it's called "post-increment"). – Marc.2377 Feb 02 '17 at 04:49
  • 1
    Agree, when you ask what is 'Optimized' everyone will have their own view. What suits you and your requirement is important. Plus, how many changes you expect plays an important role. – A3006 Feb 02 '17 at 04:51
  • [deckIndex++] is nice, didnt know that was possible, thanks – NOP da CALL Feb 02 '17 at 04:51
  • [deckIndex++] is a better way since it is dynamic than manually adding +1, +2, +3. – jace Feb 02 '17 at 04:57
  • 1
    The first one actually seems faster because as @StudentT mentioned it has less assignments. Also http://stackoverflow.com/questions/13383407/is-add-1-really-faster-than-inc-x86 – Slai Feb 02 '17 at 05:14

1 Answers1

2

The real answer is that's the wrong part to optimize, but it can be a tiny bit faster if the deck is shuffled:

Array.Copy(getDeck, CARDS_PER_HAND * 0, playerHand  , 0, CARDS_PER_HAND);
Array.Copy(getDeck, CARDS_PER_HAND * 1, hellmuthHand, 0, CARDS_PER_HAND);
Array.Copy(getDeck, CARDS_PER_HAND * 2, dwanHand    , 0, CARDS_PER_HAND);
Array.Copy(getDeck, CARDS_PER_HAND * 3, iveyHand    , 0, CARDS_PER_HAND);
Array.Copy(getDeck, CARDS_PER_HAND * 4, negreanuHand, 0, CARDS_PER_HAND);
deckIndex += CARDS_PER_HAND * 5;
Slai
  • 22,144
  • 5
  • 45
  • 53
  • the deck is shuffled and I dont understand this – NOP da CALL Feb 02 '17 at 05:50
  • @NOPdaCALL for example if `CARDS_PER_HAND` is 5, it copies the first 5 items from `getDeck` to `playerHand` instead of every 5th item. – Slai Feb 02 '17 at 06:01
  • Had a look at Array.Copy, quite cool and I see whats happening now. Was about to say that other functions depended on the updated count of deckIndex but you have appended that now and this works well. Only downside is in the looks department but thats just my opinion. – NOP da CALL Feb 02 '17 at 06:05
  • 1
    @NOPdaCALL I agree that it looks a bit worse and also might be harder to maintain and understand by others reading your code. That is why the general rule is to go with whatever is more readable/maintainable/easier, and optimize only when really needed. – Slai Feb 02 '17 at 06:15