Kay Goossen Kay Goossen - 2 months ago 7x
C++ Question

c++ Reading the first few bytes out of every X bytes efficiently

I want to read the first 16 bytes out of every X*16 bytes of a file. The code I wrote works, but is quite slow, because of many function calls.

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);

Could someone give me some suggestions to increase the performance of this code?

Nim Nim

Don't use seek, I would mmap this whole file and then simply read off the bytes at the desired locations.

I'm not going to write the code for you, but it should be along the lines of:

  1. Open the file, calculate the file size
  2. mmap the whole file
  3. Iterate through in your step sizes calculating the address of each block
  4. Construct each Vertex based on the block and push into the vector
  5. return the vector.