处理大型图像文本文件 我使用C++,拥有一个1234到1234个文本文件,值为0到255。我一直在尝试加速我的代码,因为它与用户实时使用。现在,运行需要0.5秒,将文本文件读取到向量需要0.4秒。我正在使用getline然后istringstream。下面是我目前使用的代码。这里有一些东西,我去掉了第一列和最后50列,并将第一个行块放到一个向量中,第二个行块放到另一个向量中,因为我需要它来进行处理 void readInRawData(string fileName, int start, int split, int finish, vector< vector <int> > &rawArrayTop, vector< vector <int> > &rawArrayBottom) { string line; vector<int> rawRow; int counter=0; int value=0; int numberOfColumns=0, numberOfRows=0; ifstream rawImage; rawImage.open(fileName.c_str()); //open file using fileName if (rawImage.is_open()&&!is_empty(rawImage)) { int length=0; getline(rawImage,line); istringstream ss(line); while(ss>>value)//clump into values between spaces { length++; } while(getline(rawImage, line))//get row { if(counter<start) { } else { break; } counter++; } while(getline(rawImage, line))//get row { if(counter<split) { rawRow.clear(); istringstream ss(line); for(int i=0;i<50;i++) { ss>>value; } for(int i=0; i<length-100; i++) { ss>>value; rawRow.push_back(value); } rawArrayTop.push_back(rawRow); } else { break; } counter++; } while(getline(rawImage, line))//get row { if(counter<finish) { rawRow.clear(); istringstream ss(line); for(int i=0;i<50;i++) { ss>>value; } for(int i=0; i<length-100; i++) { ss>>value; rawRow.push_back(value); } rawArrayBottom.push_back(rawRow); } else { break; } counter++; } rawImage.close(); } //if it can't be opened throw error else { throw rawArrayTop; } } void readInRawData(字符串文件名、int start、int split、int finish、vector和rawArrayTop、vector和rawArrayBottom) { 弦线; 矢量罗罗; int计数器=0; int值=0; int numberOfColumns=0,numberOfRows=0; ifstream图像; rawmimage.open(fileName.c_str());//使用fileName打开文件 if(rawImage.is_open()&&!is_empty(rawImage)) { 整数长度=0; getline(rawImage,line); istringstream ss(线); while(ss>>value)//在空格之间聚集成值 { 长度++; } while(getline(rawImage,line))//获取行 { 如果(对数值); rawRow。推回(值); } 拉瓦雷托普。推回(拉瓦罗); } 其他的 { 打破 } 计数器++; } while(getline(rawImage,line))//获取行 { 如果(对数值); } 对于(int i=0;i>值; rawRow。推回(值); } 拉瓦雷贝托姆。推回(拉瓦罗); } 其他的 { 打破 } 计数器++; } rawImage.close(); } //如果无法打开,则抛出错误 其他的 { 扔掉拉瓦雷托普; } }

处理大型图像文本文件 我使用C++,拥有一个1234到1234个文本文件,值为0到255。我一直在尝试加速我的代码,因为它与用户实时使用。现在,运行需要0.5秒,将文本文件读取到向量需要0.4秒。我正在使用getline然后istringstream。下面是我目前使用的代码。这里有一些东西,我去掉了第一列和最后50列,并将第一个行块放到一个向量中,第二个行块放到另一个向量中,因为我需要它来进行处理 void readInRawData(string fileName, int start, int split, int finish, vector< vector <int> > &rawArrayTop, vector< vector <int> > &rawArrayBottom) { string line; vector<int> rawRow; int counter=0; int value=0; int numberOfColumns=0, numberOfRows=0; ifstream rawImage; rawImage.open(fileName.c_str()); //open file using fileName if (rawImage.is_open()&&!is_empty(rawImage)) { int length=0; getline(rawImage,line); istringstream ss(line); while(ss>>value)//clump into values between spaces { length++; } while(getline(rawImage, line))//get row { if(counter<start) { } else { break; } counter++; } while(getline(rawImage, line))//get row { if(counter<split) { rawRow.clear(); istringstream ss(line); for(int i=0;i<50;i++) { ss>>value; } for(int i=0; i<length-100; i++) { ss>>value; rawRow.push_back(value); } rawArrayTop.push_back(rawRow); } else { break; } counter++; } while(getline(rawImage, line))//get row { if(counter<finish) { rawRow.clear(); istringstream ss(line); for(int i=0;i<50;i++) { ss>>value; } for(int i=0; i<length-100; i++) { ss>>value; rawRow.push_back(value); } rawArrayBottom.push_back(rawRow); } else { break; } counter++; } rawImage.close(); } //if it can't be opened throw error else { throw rawArrayTop; } } void readInRawData(字符串文件名、int start、int split、int finish、vector和rawArrayTop、vector和rawArrayBottom) { 弦线; 矢量罗罗; int计数器=0; int值=0; int numberOfColumns=0,numberOfRows=0; ifstream图像; rawmimage.open(fileName.c_str());//使用fileName打开文件 if(rawImage.is_open()&&!is_empty(rawImage)) { 整数长度=0; getline(rawImage,line); istringstream ss(线); while(ss>>value)//在空格之间聚集成值 { 长度++; } while(getline(rawImage,line))//获取行 { 如果(对数值); rawRow。推回(值); } 拉瓦雷托普。推回(拉瓦罗); } 其他的 { 打破 } 计数器++; } while(getline(rawImage,line))//获取行 { 如果(对数值); } 对于(int i=0;i>值; rawRow。推回(值); } 拉瓦雷贝托姆。推回(拉瓦罗); } 其他的 { 打破 } 计数器++; } rawImage.close(); } //如果无法打开,则抛出错误 其他的 { 扔掉拉瓦雷托普; } },c++,performance,text-files,C++,Performance,Text Files,要真正提高性能,您必须完全重写 while((ch = fgetc(fp)) != EOF) { if(isdigit(ch)) { sample = sample * 10 + ch - '0'; onsample = 1; } else { if(onsample) { *out++ = sample; sample = 0;

要真正提高性能,您必须完全重写

  while((ch = fgetc(fp)) != EOF)
  {
     if(isdigit(ch))
     {
       sample = sample * 10 + ch - '0';
       onsample = 1;
     }
     else
     {
        if(onsample)
        {
           *out++ = sample;
           sample = 0;
           onsample = 0;
        }
     }

  }

使用malloc(width*height)设置。现在,它应该以几乎可以读取的速度快速浏览文件。

我不会给出代码,但我会建议如何继续:

  • 解析文本需要很长时间。如果实时性很重要,请将文件预处理为二进制格式,因为它可以通过读/写函数直接加载。您需要从二进制文件创建二进制模式的流,并使用istream::read

  • 尽量避免使用
    vector
    ,除非您使用作用域分配器,我假设您没有使用。这对缓存有害。使用具有n*m保留空间的向量更合适

  • 如果需要二维访问,只需为函数编写代码即可:

    using Matrix = vector<int>;
    
    int & idx(Matrix, size_t row, size_t col);
    
    Matrix mat(m * n);
    
    idx(mat, 2, 3) = 17;
    
    使用矩阵=向量;
    int&idx(矩阵、大小行、大小列);
    矩阵mat(m*n);
    idx(mat,2,3)=17;
    
    另一个问题是必须加载到矩阵中。如果要避免冗余初始化,同时在加载数据之前预保留内存,stl向量不可能做到这一点,但您可以使用,它有一个重载用于保留默认的_init _t。这将不会触发向量中元素的初始化

  • 如果值介于0和255之间,请使用char,而不是int。您将在缓存中一次容纳更多数据

  • 请发布用于构建应用程序的编译器选项(优化设置)。如果您正在运行“调试”或者未优化的构建,您显示的数字基本上是没有意义的。因为您事先知道所有向量的大小,所以使用reserve来减少内存重新分配。我说过它将减少内存重新分配。这是一个事实,不涉及猜测。如果性能很重要,那么您使用的文本格式是错误的需要二进制。为什么不使用languaje c的函数fread?