最近项目的计算节点用的最多的是一个笛卡尔乘积算法,由于字典集合比较多,总高度有时会达到很高的一个量级,例如:1G。这样笛卡尔算法所 耗费的时间将很大,如何提交笛卡尔算法效率已经是当务之急。
笛卡尔乘积的算法很简单,复杂度就摆在这里O(high*n) :n指字典集合个数,high是所有集合的高度乘积。复杂度没法降低,所以我们从循环里面的运算下手,尽量降低循环内的计算,性能有所提升,下面是老版笛卡尔和新版笛卡尔的代码:
新版: 对比旧版,减少了high*(maxHigh-1)*2*n次除法,效率有所提高
int DICJob::descarte(DICComputationTask* dicTask,map<string,charset_mem*>& dicMap,uint8 segSIndex,uint8 segEIndex,uint8** resData,uint64* resHigh,int* reWidth){
#define MAX_WORD_LEN 64
cout<<"begin descarte!"<<endl;
uint64 i,j,k;// 循环变量
// maxDicHigh为所有字典库高度最大值, maxDicInx为最高字典库的下标 (均要除去star字典库)
// maxDicOffset为最高字典库在表达式口令中的偏移量, expressionLen为表达式口令长度
uint64 maxDicHigh = 0, maxDicInx = 0, maxDicOffset = 0, expressionLen = 0;
// highProduct为所有字典库高度乘积 (除去star字典库)
// outerIter为外层迭代次数, innerIter为内层迭代次数
uint64 highProduct = 1, outerIter, innerIter;
charset_mem* dicCharsetArray[MAX_WORD_LEN]={NULL};
for(int i=segSIndex; i<=segEIndex; i++){
map<string,charset_mem*>::iterator iterDic=dicMap.find(dicTask->cn_re.dicName[i]);
if(iterDic==dicMap.end()){
cout<<dicTask->cn_re