首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每次迭代后将数组写入向量会减慢程序的速度。

每次迭代后将数组写入向量会减慢程序的速度。
EN

Code Review用户
提问于 2019-12-03 15:27:06
回答 1查看 67关注 0票数 -2

我有一个C++程序,其中的输入是两个数组,每个数组都包含az数量的元素。然后,程序使用两个数组中的所有元素创建所有可能的组合。例如,如果一个数组有2^4元素,另一个数组有2^7元素,那么可能的组合将是(16 - 1) * (128 - 1) = 1,905

通过打印到控制台或将这些组合写入文件,可以在相对较快的时间内找到数组的所有组合。问题似乎是在将数组写入main.cpp末尾的向量时,因为每次迭代后,程序的速度都会大大降低。

main.cpp

代码语言:javascript
复制
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());
 }
}
}

能否避免经济放缓?

这是程序的核心脚本,并从其他文件调用其他函数,但我不认为它们是相关的,但可以在必要时包含它们。

EN

回答 1

Code Review用户

发布于 2019-12-04 00:37:08

这个代码太复杂了(看上去像C)。

将文件读入双向量的步骤。

代码语言:javascript
复制
#include <vector>
#include <iterator>
#include <fstream>

...

std::ifstream         file("filename");
std::vector<double>   data(std::istream_iterator<double>{file},
                           std::istream_iterator<double>{});

好了。

向量调整大小并复制内容从旧的数据空间到新的数据空间,如果它们没有空间。为了防止这种情况,您可以为所有元素保留最高空间,这样就不会发生重新分配。

代码语言:javascript
复制
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);

这些行没有任何用处:

代码语言:javascript
复制
std::fill(itemZ.begin(),itemZ.end(),0.0);
std::fill(itemA.begin(),itemA.end(),0.0);

这一点,这两个向量都有零个元素,因此它什么也不做。

这不是C++

代码语言:javascript
复制
printf("(%s,%s)\n",zCombo.c_str(),aCombo.c_str());

停止使用其他语言的特性。C++有更好的控制台输出操作。

代码语言:javascript
复制
std::cout << "(" << zCombo << "," << aCombo << ")\n";
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/233344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档