记一次工作中的代码优化(C++)

背景及优化原因

  1. 最近做一个需求涉及到改动原来同事写的模块代码;
  2. 写的代码有点搓,基本上给我的感觉就是:代码能跑、不出问题就行;
  3. 有事可做,减少失业率。

优化前:原来代码

主要需要优化的代码如下:

// ======== set 转 vector ========
std::set<int64_t>::iterator it;
for(it = setSupplierSkuIdList.begin(); it != setSupplierSkuIdList.end(); it++)
{vecSupplierSkuIdList.push_back(*it);
}
for(it = setSkuIdList.begin(); it != setSkuIdList.end(); it++)
{vecSkuIdList.push_back(*it);
}// 打印日志
MD_COMM_LOG_DEBUG("SendGetComSupplierSkuInfoList start.size:%zu size:%zu", vecSupplierSkuIdList.size(),vecSkuIdList.size());
MD_COMM_LOG_DEBUG("*************setStrAoRelation.size:%zu, setSupplierSkuIdList.size:%zu, setSkuIdList.size:%zu", setStrAoRelation.size(), setSupplierSkuIdList.size(), setSkuIdList.size());// 实现:将 vecSupplierSkuIdList 分200个一批去调 SendGetComSupplierSkuInfoList 接口
// 这里分 200 个一批的原因是对方接口 SendGetComSupplierSkuInfoList 有限制入参的大小为 200
if(vecSupplierSkuIdList.size() > 200)
{int32_t count = 0;vector<int64_t> vecSupplierSkuIdListNew;vector<int64_t>::const_iterator iterSupSkuId = vecSupplierSkuIdList.begin();for(;iterSupSkuId != vecSupplierSkuIdList.end(); iterSupSkuId++){vecSupplierSkuIdListNew.push_back(*iterSupSkuId);count++;if(count==200){// 异步调接口iRetCode = m_pSupplierStub->SendGetComSupplierSkuInfoList(m_oCntlInfo,m_strMachineKey,STRSOURCE,nSceneId,lOption,vecSupplierSkuIdListNew,vecSkuIdList,strInReserve);if (iRetCode != 0){MD_COMM_LOG_ERR(iRetCode, "Call GetComSupplierSkuInfoList Failed. Errmsg:[%s]",m_pSupplierStub->GetLastErrMsg());iRetCode = 0;}else{iAsyncReqestCount++;}MD_COMM_LOG_DEBUG("******vecSupplierSkuIdListNew size:%u, count:%u", vecSupplierSkuIdListNew.size(), count);vecSupplierSkuIdListNew.clear();count = 0;}}if(count > 0){MD_COMM_LOG_DEBUG("******vecSupplierSkuIdListNew size:%u, count:%u", vecSupplierSkuIdListNew.size(), count);iRetCode = m_pSupplierStub->SendGetComSupplierSkuInfoList(m_oCntlInfo,m_strMachineKey,STRSOURCE,nSceneId,lOption,vecSupplierSkuIdListNew,vecSkuIdList,strInReserve);if (iRetCode != 0){MD_COMM_LOG_ERR(iRetCode, "Call GetComSupplierSkuInfoList Failed. Errmsg:[%s]",m_pSupplierStub->GetLastErrMsg());iRetCode = 0;}else{iAsyncReqestCount++;}}
}
else
{iRetCode = m_pSupplierStub->SendGetComSupplierSkuInfoList(m_oCntlInfo,m_strMachineKey,STRSOURCE,nSceneId,lOption,vecSupplierSkuIdList,vecSkuIdList,strInReserve);if (iRetCode != 0){MD_COMM_LOG_ERR(iRetCode, "Call GetComSupplierSkuInfoList Failed. Errmsg:[%s]",m_pSupplierStub->GetLastErrMsg());iRetCode = 0;}else{iAsyncReqestCount++;}
} 

优化后:版本一

// 异步调接口的地方抽出一个函数:_GetComSerSkuInfoList
int32_t Cao_victory::_GetComSerSkuInfoList(const std::string& strSource,const std::vector<int64_t>& vecSupplierSkuIdList,const std::vector<int64_t>& vecSkuIdList,int32_t& iAsyncReqestCount)
{int32_t iRetCode = 0, nSceneId = 0;int64_t lOption = 0;string strInReserve;// 异步调接口iRetCode = m_pSupplierStub->SendGetComSerSkuInfoList(m_oCntlInfo,m_strMachineKey,STRSOURCE,nSceneId,lOption,vecSupplierSkuIdList,vecSkuIdList,strInReserve);if (iRetCode != 0){MD_COMM_LOG_ERR(iRetCode, "Call GetComSupplierSkuInfoList Failed. Errmsg:[%s]",m_pSupplierStub->GetLastErrMsg());iRetCode = 0;}else{iAsyncReqestCount++;}return iRetCode;
}// ======== set 转 vector  ========
std::set<int64_t>::iterator it;
for(it = setSupplierSkuIdList.begin(); it != setSupplierSkuIdList.end(); it++)
{vecSupplierSkuIdList.push_back(*it);
}
for(it = setSkuIdList.begin(); it != setSkuIdList.end(); it++)
{vecSkuIdList.push_back(*it);
}vector<int64_t> vecSupplierSkuIdListNew;
int32_t needSupSkuIdNum = vecSupplierSkuIdList.size();
while (needSupSkuIdNum > 0)
{if (needSupSkuIdNum <= 200){if (vecSupplierSkuIdListNew.size() > 0){vecSupplierSkuIdList = vecSupplierSkuIdListNew;}iRetCode = _GetComSerSkuInfoList(strSource, vecSupplierSkuIdList, vecSkuIdList, iAsyncReqestCount);MD_COMM_LOG_DEBUG("******vecSupplierSkuIdList size:%u, count:%u", vecSupplierSkuIdList.size(), needSupSkuIdNum);needSupSkuIdNum = 0;}else{int32_t count = 0;vector<int64_t>::const_iterator iterSupSkuId = vecSupplierSkuIdList.begin();for(;iterSupSkuId != vecSupplierSkuIdList.end(); iterSupSkuId++){vecSupplierSkuIdListNew.push_back(*iterSupSkuId);count++;if(count==200){iRetCode = _GetComSerSkuInfoList(strSource, vecSupplierSkuIdListNew, vecSkuIdList, iAsyncReqestCount);MD_COMM_LOG_DEBUG("******vecSupplierSkuIdListNew size:%u, count:%u", vecSupplierSkuIdListNew.size(), count);vecSupplierSkuIdListNew.clear();count = 0;needSupSkuIdNum -= 200;}}}}

优化后:版本二(最终版)

std::set<int64_t>::iterator it = setSupplierSkuIdList.begin();
vector<int64_t>  vecSkuIdList(setSkuIdList.begin(),setSkuIdList.end());MD_COMM_LOG_DEBUG("*************setStrAoRelation.size:%zu, setSupplierSkuIdList.size:%zu, setSkuIdList.size:%zu", setStrAoRelation.size(), setSupplierSkuIdList.size(), setSkuIdList.size());while ( it != setSupplierSkuIdList.end())
{int leftSkuNum = std::distance(it, setSupplierSkuIdList.end());if (leftSkuNum <= 200){vector<int64_t> vecSupplierSkuIdList(it, setSupplierSkuIdList.end());it = setSupplierSkuIdList.end();iRetCode = _GetComSupplierSkuInfoList(strSource, vecSupplierSkuIdList, vecSkuIdList, iAsyncReqestCount);MD_COMM_LOG_DEBUG("******vecSupplierSkuIdList size:%zu, count:%d", vecSupplierSkuIdList.size(), leftSkuNum);}else{std::set<int64_t>::iterator endIt = it;std::advance(endIt,200);vector<int64_t> vecSupplierSkuIdList(it, endIt);it = endIt;iRetCode = _GetComSupplierSkuInfoList(strSource, vecSupplierSkuIdList, vecSkuIdList, iAsyncReqestCount);MD_COMM_LOG_DEBUG("******vecSupplierSkuIdList size:%zu, count:%d", vecSupplierSkuIdList.size(), leftSkuNum);}}

总结

优化后版本二显得更见的简洁,其中用到了 C++advance,参考和学习地址如下:advance - C++ Reference。

大家如有更好的优化方法,可以留言评论~,也欢迎关注公众号:“Go键盘侠”,一起学习共勉!

 

记一次工作中的代码优化(C++)

背景及优化原因

  1. 最近做一个需求涉及到改动原来同事写的模块代码;
  2. 写的代码有点搓,基本上给我的感觉就是:代码能跑、不出问题就行;
  3. 有事可做,减少失业率。

优化前:原来代码

主要需要优化的代码如下:

// ======== set 转 vector ========
std::set<int64_t>::iterator it;
for(it = setSupplierSkuIdList.begin(); it != setSupplierSkuIdList.end(); it++)
{vecSupplierSkuIdList.push_back(*it);
}
for(it = setSkuIdList.begin(); it != setSkuIdList.end(); it++)
{vecSkuIdList.push_back(*it);
}// 打印日志
MD_COMM_LOG_DEBUG("SendGetComSupplierSkuInfoList start.size:%zu size:%zu", vecSupplierSkuIdList.size(),vecSkuIdList.size());
MD_COMM_LOG_DEBUG("*************setStrAoRelation.size:%zu, setSupplierSkuIdList.size:%zu, setSkuIdList.size:%zu", setStrAoRelation.size(), setSupplierSkuIdList.size(), setSkuIdList.size());// 实现:将 vecSupplierSkuIdList 分200个一批去调 SendGetComSupplierSkuInfoList 接口
// 这里分 200 个一批的原因是对方接口 SendGetComSupplierSkuInfoList 有限制入参的大小为 200
if(vecSupplierSkuIdList.size() > 200)
{int32_t count = 0;vector<int64_t> vecSupplierSkuIdListNew;vector<int64_t>::const_iterator iterSupSkuId = vecSupplierSkuIdList.begin();for(;iterSupSkuId != vecSupplierSkuIdList.end(); iterSupSkuId++){vecSupplierSkuIdListNew.push_back(*iterSupSkuId);count++;if(count==200){// 异步调接口iRetCode = m_pSupplierStub->SendGetComSupplierSkuInfoList(m_oCntlInfo,m_strMachineKey,STRSOURCE,nSceneId,lOption,vecSupplierSkuIdListNew,vecSkuIdList,strInReserve);if (iRetCode != 0){MD_COMM_LOG_ERR(iRetCode, "Call GetComSupplierSkuInfoList Failed. Errmsg:[%s]",m_pSupplierStub->GetLastErrMsg());iRetCode = 0;}else{iAsyncReqestCount++;}MD_COMM_LOG_DEBUG("******vecSupplierSkuIdListNew size:%u, count:%u", vecSupplierSkuIdListNew.size(), count);vecSupplierSkuIdListNew.clear();count = 0;}}if(count > 0){MD_COMM_LOG_DEBUG("******vecSupplierSkuIdListNew size:%u, count:%u", vecSupplierSkuIdListNew.size(), count);iRetCode = m_pSupplierStub->SendGetComSupplierSkuInfoList(m_oCntlInfo,m_strMachineKey,STRSOURCE,nSceneId,lOption,vecSupplierSkuIdListNew,vecSkuIdList,strInReserve);if (iRetCode != 0){MD_COMM_LOG_ERR(iRetCode, "Call GetComSupplierSkuInfoList Failed. Errmsg:[%s]",m_pSupplierStub->GetLastErrMsg());iRetCode = 0;}else{iAsyncReqestCount++;}}
}
else
{iRetCode = m_pSupplierStub->SendGetComSupplierSkuInfoList(m_oCntlInfo,m_strMachineKey,STRSOURCE,nSceneId,lOption,vecSupplierSkuIdList,vecSkuIdList,strInReserve);if (iRetCode != 0){MD_COMM_LOG_ERR(iRetCode, "Call GetComSupplierSkuInfoList Failed. Errmsg:[%s]",m_pSupplierStub->GetLastErrMsg());iRetCode = 0;}else{iAsyncReqestCount++;}
} 

优化后:版本一

// 异步调接口的地方抽出一个函数:_GetComSerSkuInfoList
int32_t Cao_victory::_GetComSerSkuInfoList(const std::string& strSource,const std::vector<int64_t>& vecSupplierSkuIdList,const std::vector<int64_t>& vecSkuIdList,int32_t& iAsyncReqestCount)
{int32_t iRetCode = 0, nSceneId = 0;int64_t lOption = 0;string strInReserve;// 异步调接口iRetCode = m_pSupplierStub->SendGetComSerSkuInfoList(m_oCntlInfo,m_strMachineKey,STRSOURCE,nSceneId,lOption,vecSupplierSkuIdList,vecSkuIdList,strInReserve);if (iRetCode != 0){MD_COMM_LOG_ERR(iRetCode, "Call GetComSupplierSkuInfoList Failed. Errmsg:[%s]",m_pSupplierStub->GetLastErrMsg());iRetCode = 0;}else{iAsyncReqestCount++;}return iRetCode;
}// ======== set 转 vector  ========
std::set<int64_t>::iterator it;
for(it = setSupplierSkuIdList.begin(); it != setSupplierSkuIdList.end(); it++)
{vecSupplierSkuIdList.push_back(*it);
}
for(it = setSkuIdList.begin(); it != setSkuIdList.end(); it++)
{vecSkuIdList.push_back(*it);
}vector<int64_t> vecSupplierSkuIdListNew;
int32_t needSupSkuIdNum = vecSupplierSkuIdList.size();
while (needSupSkuIdNum > 0)
{if (needSupSkuIdNum <= 200){if (vecSupplierSkuIdListNew.size() > 0){vecSupplierSkuIdList = vecSupplierSkuIdListNew;}iRetCode = _GetComSerSkuInfoList(strSource, vecSupplierSkuIdList, vecSkuIdList, iAsyncReqestCount);MD_COMM_LOG_DEBUG("******vecSupplierSkuIdList size:%u, count:%u", vecSupplierSkuIdList.size(), needSupSkuIdNum);needSupSkuIdNum = 0;}else{int32_t count = 0;vector<int64_t>::const_iterator iterSupSkuId = vecSupplierSkuIdList.begin();for(;iterSupSkuId != vecSupplierSkuIdList.end(); iterSupSkuId++){vecSupplierSkuIdListNew.push_back(*iterSupSkuId);count++;if(count==200){iRetCode = _GetComSerSkuInfoList(strSource, vecSupplierSkuIdListNew, vecSkuIdList, iAsyncReqestCount);MD_COMM_LOG_DEBUG("******vecSupplierSkuIdListNew size:%u, count:%u", vecSupplierSkuIdListNew.size(), count);vecSupplierSkuIdListNew.clear();count = 0;needSupSkuIdNum -= 200;}}}}

优化后:版本二(最终版)

std::set<int64_t>::iterator it = setSupplierSkuIdList.begin();
vector<int64_t>  vecSkuIdList(setSkuIdList.begin(),setSkuIdList.end());MD_COMM_LOG_DEBUG("*************setStrAoRelation.size:%zu, setSupplierSkuIdList.size:%zu, setSkuIdList.size:%zu", setStrAoRelation.size(), setSupplierSkuIdList.size(), setSkuIdList.size());while ( it != setSupplierSkuIdList.end())
{int leftSkuNum = std::distance(it, setSupplierSkuIdList.end());if (leftSkuNum <= 200){vector<int64_t> vecSupplierSkuIdList(it, setSupplierSkuIdList.end());it = setSupplierSkuIdList.end();iRetCode = _GetComSupplierSkuInfoList(strSource, vecSupplierSkuIdList, vecSkuIdList, iAsyncReqestCount);MD_COMM_LOG_DEBUG("******vecSupplierSkuIdList size:%zu, count:%d", vecSupplierSkuIdList.size(), leftSkuNum);}else{std::set<int64_t>::iterator endIt = it;std::advance(endIt,200);vector<int64_t> vecSupplierSkuIdList(it, endIt);it = endIt;iRetCode = _GetComSupplierSkuInfoList(strSource, vecSupplierSkuIdList, vecSkuIdList, iAsyncReqestCount);MD_COMM_LOG_DEBUG("******vecSupplierSkuIdList size:%zu, count:%d", vecSupplierSkuIdList.size(), leftSkuNum);}}

总结

优化后版本二显得更见的简洁,其中用到了 C++advance,参考和学习地址如下:advance - C++ Reference。

大家如有更好的优化方法,可以留言评论~,也欢迎关注公众号:“Go键盘侠”,一起学习共勉!