static const int nCanUseCampCnt = 2; int nUseCampList[nCanUseCampCnt] = { pSeleGroup->bGroupCamp, ccCardCamp6 }; for (int nCampIdx = 0; nCampIdx < nCanUseCampCnt; ++nCampIdx) { int nCampId = nUseCampList[nCampIdx]; CUserCardContainer& vSeleCampList = pPlayInfo->m_NewPlayCardBag[nCampId]; for (INT_PTR i = 0; i < vSeleCampList.count(); ++i) { auto pUserCard = vSeleCampList[i]; if (pUserCard == NULL) continue; bool bIsCanAdd = true; for (int groupCardIdx = 0; groupCardIdx < INIT_FIGHT_OUT_COUNT; ++groupCardIdx) { if (pSeleGroup->wCardIdList[groupCardIdx] == pUserCard->wCardId) { bIsCanAdd = false; break; } } if (!bIsCanAdd || IsBeProhCard(pUserCard->wCardId)) continue; if (nCampId == ccCardCamp6) { pPlayInfo->vCanSuppNeutralCardList.insert(pUserCard->wCardId); } else { pPlayInfo->vCanSuppCardList.insert(pUserCard->wCardId); } } } 帮我优化下性能
时间: 2024-03-05 09:52:57 浏览: 21
在这段代码中,我们可以通过以下几个方式来优化性能:
1. 避免重复计算 `nCanUseCampCnt`。我们可以将其定义为常量,以便在整个程序中使用,而不必每次执行循环都重新计算。
2. 避免多余的数组拷贝。我们可以在循环之前将 `pSeleGroup->bGroupCamp` 和 `ccCardCamp6` 直接存储在一个数组中,而不必使用 `nUseCampList` 数组。
3. 避免在循环中重复访问 `vSeleCampList` 对象的 `count()` 方法。我们可以将其存储在一个变量中,以便在整个循环中重复使用。
4. 避免在循环中多次访问 `vSeleCampList` 对象。我们可以在循环之前将其存储在一个变量中,并在循环中重复使用。
5. 避免在循环中多次访问 `pSeleGroup->wCardIdList` 数组。我们可以将其存储在一个变量中,并在循环中重复使用。
6. 避免在循环中多次访问 `IsBeProhCard()` 函数。我们可以将其存储在一个变量中,并在循环中重复使用。
7. 避免不必要的插入操作。我们可以将 `pUserCard->wCardId` 存储在一个变量中,并在插入操作之前进行判断。
下面是优化后的代码:
```c++
static const int nCanUseCampCnt = 2;
const int nUseCampList[nCanUseCampCnt] = { pSeleGroup->bGroupCamp, ccCardCamp6 };
for (int nCampIdx = 0; nCampIdx < nCanUseCampCnt; ++nCampIdx)
{
int nCampId = nUseCampList[nCampIdx];
CUserCardContainer& vSeleCampList = pPlayInfo->m_NewPlayCardBag[nCampId];
int nCount = vSeleCampList.count();
const WORD* pCardIdList = pSeleGroup->wCardIdList;
bool (*pIsBeProhCard)(WORD) = &IsBeProhCard;
std::unordered_set<WORD>& vCanSuppCardList = (nCampId == ccCardCamp6) ? pPlayInfo->vCanSuppNeutralCardList : pPlayInfo->vCanSuppCardList;
for (INT_PTR i = 0; i < nCount; ++i)
{
auto pUserCard = vSeleCampList[i];
if (pUserCard == nullptr) continue;
WORD wCardId = pUserCard->wCardId;
bool bIsCanAdd = true;
for (int groupCardIdx = 0; groupCardIdx < INIT_FIGHT_OUT_COUNT; ++groupCardIdx)
{
if (pCardIdList[groupCardIdx] == wCardId)
{
bIsCanAdd = false;
break;
}
}
if (!bIsCanAdd || pIsBeProhCard(wCardId)) continue;
vCanSuppCardList.insert(wCardId);
}
}
```
希望这些优化能够对你有所帮助。如果你有其他问题或需要进一步的优化,请随时告诉我。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)