读取无符号字符数组数组中充满字符串(以换行符分隔)的文件的最快方法是什么(反之亦然)?

发布于 2024-12-11 09:45:41 字数 1724 浏览 0 评论 0原文

好吧,也许标题有点误导。我希望您做的就是快速浏览以下两个片段,并给我一些关于如何在不使用太奇特的代码的情况下尽可能提高性能的提示。该代码仅需要在 win32 上运行。不幸的是,STL 容器目前还不是一个选择。

读取文件……

bool TextFile::Read(const char *pFilePath)
{
    bool            bSuccess    = false;
    std::ifstream   oFile(pFilePath, std::ios_base::in);

    if(oFile.is_open())
    {
        std::string stLineNow;
        std::size_t siLineLength;

        if(this->pLines)
        {
            this->Clear();
        }

        this->stFilePath = pFilePath;

        oFile.seekg(0, std::ios::end);
        this->pLines = new unsigned char *[static_cast<unsigned int> (oFile.tellg())];
        oFile.seekg(0, std::ios::beg);

        for(this->ulLinesCount = 0; std::getline(oFile, stLineNow).good(); this->ulLinesCount++)
        {
            siLineLength = stLineNow.length() + 1;
            this->pLines[this->ulLinesCount] = new unsigned char[siLineLength];
            memcpy(this->pLines[this->ulLinesCount], stLineNow.c_str(), siLineLength);
        }

        bSuccess = true;

        oFile.close();
    }

    return bSuccess;
}

并保存它……

bool TextFile::Save(const char *pFilePath)
{
    bool bSuccess = false;

    if(this->pLines)
    {
        std::ofstream oFile(pFilePath ? pFilePath : this->stFilePath, std::ios_base::out);

        if(oFile.is_open())
        {
            for(unsigned long ulPosition = 0; ulPosition < this->GetCount(); ulPosition++)
            {
                oFile << this->Get(ulPosition) << '\n';
            }

            bSuccess = true;

            oFile.close();
        }
    }

    return bSuccess;
}

请原谅丑陋的格式。

提前致谢!

Well, maybe the title is misleading a bit. All I want you to do is to take a quick look at the following two snippets and to give me some tips on how to improve the performance as much as possible without getting too exotic code. The code is needed to function on win32 only. Unfortunately STL containers are not an option right now.

To read the file...

bool TextFile::Read(const char *pFilePath)
{
    bool            bSuccess    = false;
    std::ifstream   oFile(pFilePath, std::ios_base::in);

    if(oFile.is_open())
    {
        std::string stLineNow;
        std::size_t siLineLength;

        if(this->pLines)
        {
            this->Clear();
        }

        this->stFilePath = pFilePath;

        oFile.seekg(0, std::ios::end);
        this->pLines = new unsigned char *[static_cast<unsigned int> (oFile.tellg())];
        oFile.seekg(0, std::ios::beg);

        for(this->ulLinesCount = 0; std::getline(oFile, stLineNow).good(); this->ulLinesCount++)
        {
            siLineLength = stLineNow.length() + 1;
            this->pLines[this->ulLinesCount] = new unsigned char[siLineLength];
            memcpy(this->pLines[this->ulLinesCount], stLineNow.c_str(), siLineLength);
        }

        bSuccess = true;

        oFile.close();
    }

    return bSuccess;
}

... and to save it...

bool TextFile::Save(const char *pFilePath)
{
    bool bSuccess = false;

    if(this->pLines)
    {
        std::ofstream oFile(pFilePath ? pFilePath : this->stFilePath, std::ios_base::out);

        if(oFile.is_open())
        {
            for(unsigned long ulPosition = 0; ulPosition < this->GetCount(); ulPosition++)
            {
                oFile << this->Get(ulPosition) << '\n';
            }

            bSuccess = true;

            oFile.close();
        }
    }

    return bSuccess;
}

... and please excuse the ugly formatting.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里泪两行 2024-12-18 09:45:41

嗯...我会做的是:

  • 使用内存映射文件。这是读取文件最快的方式;
  • 然后 memcpy() 将文件的所有内容放入新的缓冲区中;
  • 然后运行一次缓冲区并计算换行符;
  • 创建一个由与换行符一样多的指针组成的数组;
  • 再次运行缓冲区,这次:
    • 将每个换行符的第一个字节替换为 0
    • 将指针放置到指针数组中每行的开头。

瞧!

Well... what I would do is:

  • Use memory-mapped files. That's THE fastest way of reading files;
  • Then memcpy() all the file's contents in a new buffer;
  • Then run through the buffer once and count the newlines;
  • Make an array of as many pointers as there are newlines;
  • Run through the buffer another time, and this time:
    • Replace the first byte of each newline with 0.
    • Place a pointer to the start of each line in the pointer array.

Voilà !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文