小编Kay*_*sen的帖子

c ++有效地读取每个X字节中的前几个字节

我想读取文件的每个X*16字节中的前16个字节.我编写的代码可以工作,但由于函数调用很多,所以代码很慢.

std::vector<Vertex> readFile(int maxVertexCount) {
    std::ifstream in = std::ifstream(fileName, std::ios::binary);
    in.seekg(0, in.end);
    int fileLength = in.tellg();
    int vertexCount = fileLength / 16;

    int stepSize = std::max(1, vertexCount / maxVertexCount);

    std::vector<Vertex> vertices;
    vertices.reserve(vertexCount / stepSize);
    for (int i = 0; i < vertexCount; i += stepSize) {
        in.seekg(i * 16, in.beg);
        char bytes[16];
        in.read(bytes, 16);
        vertices.push_back(Vertex(bytes));
    }
    in.close();
}
Run Code Online (Sandbox Code Playgroud)

有人可以给我一些建议来提高这段代码的性能吗?

c++ windows performance bytebuffer file

3
推荐指数
1
解决办法
162
查看次数

标签 统计

bytebuffer ×1

c++ ×1

file ×1

performance ×1

windows ×1