【发布时间】:2016-08-05 12:57:44
【问题描述】:
我想从文件的每个 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();
}
有人可以给我一些建议来提高这段代码的性能吗?
【问题讨论】:
标签: c++ windows performance file bytebuffer