CPP快速读取双倍的小二进制文件



从C或C++,我想尽快读取二进制格式的双精度文件。

文件很小,通常约为 100KB(顶部为 200 KB)。我希望能够:

  • 阅读双打文件。
  • 将它们转换/存储在双精度向量中
  • 循环访问向量。

如果可能的话,在此系统上执行这些操作不到 2 毫秒。目前大约在 4-6 毫秒内。

帮助但未解决问题的线程:

链接 1

链接 2 --> 这甚至没有编译。

链接 3 --> 这对双打不起作用。

链接 4 --> 这样做。

这是我的文件解析器:

"C"阅读风格:

void OfflineAnalyser::readNParseData(const char* filePath, vector<double> *&data){
    // Temporary Variables
    FILE* pFile;
    long fileSize;
    double *fileBuffer;
    size_t sizeOfBuffer;
    size_t result;
    // Open File
    pFile = fopen(filePath, "rb");
    if (pFile == NULL){
        cout << "File: " << filePath << " does not exist" << endl;
    }
    // Check whether the parameter is already full
    if (!data){
        // Reset the output
        data->clear();
        data = 0;
    }
    // Obtain file size:
    fseek(pFile, 0, SEEK_END);
    fileSize = ftell(pFile);
    rewind(pFile);
    // allocate memory to contain the whole file:
    fileBuffer = (double*)malloc(fileSize);
    if (fileBuffer == NULL) { fputs("Memory error", stderr); exit(2); }
    // copy the file into the buffer:
    result = fread(fileBuffer, 1, fileSize, pFile);
    if (result != fileSize) {
        fputs("Reading error", stderr); 
        system("pause");
        exit(3);
    }
    // the whole file is now loaded in the memory buffer.
    sizeOfBuffer = result / sizeof(double);
    // Now convert the double array into vector
    data = new vector<double>(fileBuffer, fileBuffer + sizeOfBuffer);
    free(fileBuffer);
    // terminate
    fclose(pFile);
}

方法 2:C++样式

void OfflineAnalyser::readNParseData2(const char* filePath, vector<double> *&data){
    ifstream ifs(filePath, ios::in | ios::binary);
    // If this is a valid file
    if (ifs) {
        // Temporary Variables
        std::streampos fileSize;
        double *fileBuffer;
        size_t sizeOfBuffer;
        // Check whether the parameter is already full
        if (!data){
            // Reset the output
            data->clear();
            data = 0;
        }
        // Get the size of the file
        ifs.seekg(0, std::ios::end);
        fileSize = ifs.tellg();
        ifs.seekg(0, std::ios::beg);
        sizeOfBuffer = fileSize / sizeof(double);
        fileBuffer = new double[sizeOfBuffer];
        ifs.read(reinterpret_cast<char*>(fileBuffer), fileSize);
        // Now convert the double array into vector
        data = new vector<double>(fileBuffer, fileBuffer + sizeOfBuffer);
        free(fileBuffer);
    }
}

对此代码的任何建议不胜感激。随意输入自己的代码。如果我能看到双打或istream_iterator解决方案的 std::copy,我会很高兴。

提前谢谢。

由于 vector 按顺序存储元素,因此将文件缓冲区读取到矢量的数据缓冲区更有效。

void readNParseData(const char* filePath, vector<double>& data){
    // Temporary Variables
    FILE* pFile;
    long fileSize;
    size_t result;
    // Open File
    pFile = fopen(filePath, "rb");
    if (pFile == NULL){
        cout << "File: " << filePath << " does not exist" << endl;
    }
    // Check whether the parameter is already full
    if (!data.empty()){
        data.clear();
    }
    // Obtain file size:
    fseek(pFile, 0, SEEK_END);
    fileSize = ftell(pFile);
    rewind(pFile);
    data.resize(fileSize / 8);
    if(fread(&(data[0]), 1, fileSize, pFile) != fileSize)
    {
        cout << "read error" << endl;
    }
    fclose(pFile);
}

我已经测试了您的代码和我的解决方案。当文件大小为 21KB 时,您的代码大约需要 20,000 毫秒,而我的解决方案大约需要 16 毫秒。

此外,您的代码中存在错误。 if(!data)守望if(data)

最新更新