对于这个具体案例并不重要但是什么是更好的做法,什么会更快?我认为后者对于我读过的两个都不应该对任何大于1的数字进行硬编码,并且还猜测INC会比汇编中的ADD快。虽然我认为前者看起来更好。或者有更好的方法吗?
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;
}
或
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 ++;
}
答案 0 :(得分:2)
真正的答案是,优化是错误的部分,但如果牌组改组,它可以更快一点:
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;