c ++写和读文本文件很慢,有什么选择吗?

时间:2015-08-04 15:04:46

标签: c++ filestream wifstream

我目前正在为游戏编写代码,而我在保存和加载关卡时有点困难。为了写作,我使用这段代码:

    bool WorldGen::GenerateNewWorld(unsigned int seed, int width)
{
    std::cout << "World generating..." << std::endl;
    int heigth = 1; //2D perlin noise instead of 3D
    m_WorldSizeX = width;
    m_WorldSizeY = 1800; //add a int height if implementing different world sizes

    // Create a PerlinNoise object with a random permutation vector generated with seed
    PerlinNoise(seed);

    std::vector<byte> arrMaxHeight;

    // looping through all the x locations and deciding the y value
    for (unsigned int i = 0; i < heigth; ++i) {     // y
        for(unsigned int j = 0; j < width; ++j) {  // x
            double x = (double)j / ((double)width);
            double y = (double)i / ((double)heigth);

            // Typical Perlin noise
            double n = noise(10 * x, 10 * y, 0.8);

            //n is the ground added on top of the base layer (n = highest peak at point j)
            arrMaxHeight.push_back((int)(n * 255));
        }
    }

    std::wofstream fileStream;
    fileStream.open(L"GameSave/world/World.txt");

    if (fileStream.fail())
    {
        return false;
    }

    //fileStream << L"[I could put something up here but that's still in development!]" << std::endl;

    byte blockType = 0;
    std::vector<byte> arrBlockType;

    for (int i = 0; i < m_WorldSizeX; i++)
    {
        for (int j = 0; j < m_WorldSizeY; j++)
        {
            if (j > arrMaxHeight.at(i))
            {
                //block is not air
                blockType = 1;
            }
            else
            {
                //block is air
                blockType = 0;
            }

            arrBlockType.push_back(blockType);
            fileStream << blockType << "/n";
        }
    }

    fileStream.close();

    return true;
}

现在这还不错,在大约5分钟内创建世界并将其发送到world.txt而没有任何问题,我的加载(每行读取world.txt行)然而需要很长时间。大约30多分钟,使用std::wifstream及其getline()函数完全阅读文本文件中的所有行。它读取所有行并将它们添加到std::vector,然后从该向量创建“块”。块的创建在几秒钟内完成,但是wifstream非常慢。

以下是worldLoad的代码:

    std::wifstream ifileStream;

ifileStream.open("GameSave/world/World.txt");
if (ifileStream.fail())
{
    std::cout << "Could not open World.txt" << std::endl;
    return;
}

std::wcout << "LoadWorld Started";
std::wstring extractedLine;
while (!ifileStream.eof())
{
    std::getline(ifileStream, extractedLine);

    m_ArrBlockData.push_back(StringToByte(extractedLine));
    std::wcout << m_ArrBlockData.size() << "\n";
}


DOUBLE2 location;

for (size_t i = 0; i < m_ArrBlockData.size(); i++)
{

    location.y = (i % m_WorldSizeY) * 16;
    if (location.y == 0)
    {
        location.x += 16;
    }

    Block *block = new Block(location, m_ArrBlockData.at(i));
    m_ArrBlocks.push_back(block);

    std::wcout << "Bock Created" << std::endl;
}

关于如何优化这个的任何想法?我在考虑只读取播放器周围的blockTypes,但这仍然需要我将所有块放到一个向量中才能对它们进行操作。

亲切的问候, 雅尼

PS:DOUBLE2是一个自定义变量,持有2个双打DOUBLE2(双x,双y)

3 个答案:

答案 0 :(得分:0)

将探查器附加到您的代码中,看看究竟是什么非常慢, 我怀疑它与流(那些往往很慢)和向量有关。 (当您插入数据时,它们会重新分配/移动数据)。

如果您事先知道尺寸,请在矢量中保留尺寸。

答案 1 :(得分:0)

什么是arrBlockType?随着它的增长,你可能会有很多重新分配(因为你没有预先分配它内部的任何空间)....然后你一旦填充它就永远不会真正使用它。拿出来,你的功能会更快。

答案 2 :(得分:0)

如果您可以使用提升,我建议使用其serialization library。使用二进制存档时,它易于使用,灵活且性能相对较高。当然,有很多选择。

相关问题