我有一个C++程序,其中的输入是两个数组,每个数组都包含a和z数量的元素。然后,程序使用两个数组中的所有元素创建所有可能的组合。例如,如果一个数组有2^4元素,另一个数组有2^7元素,那么可能的组合将是(16 - 1) * (128 - 1) = 1,905。
通过打印到控制台或将这些组合写入文件,可以在相对较快的时间内找到数组的所有组合。问题似乎是在将数组写入main.cpp末尾的向量时,因为每次迭代后,程序的速度都会大大降低。
main.cpp
double ZProfile1Array[MPROFILEVAL];
double AProfile1Array[MPROFILEVAL];
int solver(char *filename){
char zLine[MCOMBIN][300];
FILE *plist;
int i = 0; int stotal = 0;
// Get array of possible combinations into zLine array
char *sdfilename = getDSFileName(1);
plist = fopen(sdfilename,"r");
while(fgets(zLine[i],sizeof(zLine[i]),plist)){
zLine[i][strlen(zLine[i]) - 1] = '\0'; // gets rid of \n that fgets gets
i++;
}
fclose(plist);
stotal = i;
// Get array of possible combinations into aLine array
sdfilename = getDSFileName(2);
char aLine[MCOMBIN][300];
i = 0; int dtotal = 0;
plist = fopen(sdfilename,"r");
while(fgets(aLine[i],sizeof(aLine[i]),plist)){
aLine[i][strlen(aLine[i]) - 1] = '\0'; // gets rid of \n that fgets gets
i++;
}
fclose(plist);
dtotal = i;
std::vector<double> itemA;
std::vector<double> itemZ;
std::string aCombo;
std::string zCombo;
char *token;char *tokend;char tstr[300];
int index = 0;
int j = 0;int id = 0; int jd = 0;int indexd = 0;
// for each value of Sigma nA
for(i = 1; i < zTotal; i++){ // i = 1 because first line is always empty
zCombo = "";
for(j = 0; j < rows; j++)ZProfile1Array[j] = 0;
token = strtok(zLine[i]," ");
index = 0;
while (token){
index = atoi(token) - 1; // because array indicies start from 0
token = strtok(NULL," ");
for(j = 0; j < rows; j++){
ZProfile1Array[j] += zprofile[index][j];
}
zCombo.append(sname[index]);
zCombo.append(" + ");
}
zCombo.resize(zCombo.size() - 3); // remove trailing " + "
// for each value of Sigma nZ
for(id = 1; id < aTotal; id++){ // i = 1 because first line is always empty
aCombo = "";
for(jd = 0; jd < rows; jd++)AProfile1Array[jd] = 0;
strcpy(tstr,aLine[id]); // use strtok on tstr because will be called a number of times and we don't want to modify aLine
tokend = strtok(tstr," ");
indexd = 0;
while (tokend){
indexd = atoi(tokend) - 1; // because array indicies start from 0 and nZ starts from 1
tokend = strtok(NULL," ");
for(jd = 0; jd < rows; jd++){
AProfile1Array[jd] += aprofile[indexd][jd];
}
aCombo.append(dname[indexd]);
aCombo.append(" + ");
}
aCombo.resize(aCombo.size() - 3); // remove trailing " + "
// write array to vector
// *** next several lines slows down script ***
std::fill(itemZ.begin(),itemZ.end(),0.0);
std::fill(itemA.begin(),itemA.end(),0.0);
for(int ii = 0; ii < rows; ii++){
itemZ.push_back(ZProfile1Array[ii]);
itemA.push_back(AProfile1Array[ii]);
}
printf("(%s,%s)\n",zCombo.c_str(),aCombo.c_str());
}
}
}能否避免经济放缓?
这是程序的核心脚本,并从其他文件调用其他函数,但我不认为它们是相关的,但可以在必要时包含它们。
发布于 2019-12-04 00:37:08
这个代码太复杂了(看上去像C)。
将文件读入双向量的步骤。
#include <vector>
#include <iterator>
#include <fstream>
...
std::ifstream file("filename");
std::vector<double> data(std::istream_iterator<double>{file},
std::istream_iterator<double>{});好了。
向量调整大小并复制内容从旧的数据空间到新的数据空间,如果它们没有空间。为了防止这种情况,您可以为所有元素保留最高空间,这样就不会发生重新分配。
std::vector<double> itemA;
std::vector<double> itemZ;
// You don't specify where rows is defined or set.
// But we know that these arrays will eventually reach this size.
itemA.reserve(rows);
itemZ.reserve(rows);这些行没有任何用处:
std::fill(itemZ.begin(),itemZ.end(),0.0);
std::fill(itemA.begin(),itemA.end(),0.0);这一点,这两个向量都有零个元素,因此它什么也不做。
这不是C++
printf("(%s,%s)\n",zCombo.c_str(),aCombo.c_str());停止使用其他语言的特性。C++有更好的控制台输出操作。
std::cout << "(" << zCombo << "," << aCombo << ")\n";https://codereview.stackexchange.com/questions/233344
复制相似问题