这段代码还能优化么:pLogView = GetRightPane(); if (pLogView) { nCount = pLogView->GetListCtrl().GetItemCount(); do { pLogView->GetListCtrl().DeleteItem(0); nCount--; } while (nCount > m_nRowCount); }
时间: 2024-03-01 18:50:26 浏览: 19
这段代码可以进行如下优化:
```c++
pLogView = GetRightPane();
if (pLogView)
{
int nCount = pLogView->GetListCtrl().GetItemCount();
if (nCount > m_nRowCount)
{
for (int i = 0; i < nCount - m_nRowCount; ++i)
{
pLogView->GetListCtrl().DeleteItem(i);
}
}
}
```
主要的优化点有:
1. 如果 `nCount <= m_nRowCount`,则不需要进行删除操作,直接返回即可。
2. 不需要使用 `do-while` 循环,改用 `for` 循环。
3. 在循环内部直接使用索引进行删除,避免每次都使用 `DeleteItem(0)`,提高效率。
相关问题
void Delay_ms(uint32_t nCount) { SysTick->LOAD = 72*nCount; SysTick->VAL = 0x00; SysTick->CTRL = 0x01; while((SysTick->CTRL & 0x01) && (!(SysTick->CTRL & (1<<16)))); SysTick->CTRL = 0x00; SysTick->VAL = 0X00; }
这段代码是用于实现延时的函数,输入参数nCount是延时的毫秒数。具体实现是通过STM32的SysTick定时器来实现的。首先将延时的毫秒数乘以72,得到SysTick定时器的重装值,然后将SysTick定时器的VAL寄存器清零,将CTRL寄存器的第0位设置为1,启动SysTick定时器。在SysTick定时器运行期间,程序会一直进入while循环中,等待SysTick定时器的COUNTFLAG标志位被置位并且CTRL寄存器的第16位清零,表示定时器已经定时完成。最后将CTRL寄存器清零,将VAL寄存器清零,结束延时函数的执行。
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); } } } 帮我优化下性能
在这段代码中,我们可以通过以下几个方式来优化性能:
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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)