title: C++ Reading from several sections of a file is too slow
body: <p>I need to read byte arrays from several locations of a big file.
  I have already optimized the file so that as few sections as possible have to be read, and the sections are as closely together as possible.</p>
  
  <p>I have 20 calls like this one:</p>
  
  <pre><code>m_content.resize(iByteCount);
  
  fseek(iReadFile,iStartPos ,SEEK_SET);
  size_t readElements = fread(&amp;m_content[0], sizeof(unsigned char), iByteCount, iReadFile); 
  </code></pre>
  
  <p>iByteCount is around 5000 on average.</p>
  
  <p>Before using fread, I used a memory-mapped file, but the results were approximately the same.</p>
  
  <p>My calls are still too slow (around 200 ms) when called for the first time. When I repeat the same call with the same sections of bytes to read, it is very fast (around 1 ms), but that does not really help me.</p>
  
  <p>The file is big (around 200 mb).
  After this call, I have to read double values from a different section of the file, but I can not avoid this.</p>
  
  <p>I don't want to split it up in 2 files. I have seen the "huge file approach" used by other people, too, and they overcame this problem somehow.</p>
  
  <p>If I use memory-mapping, the first call of reading is always slow. If I then repeat reading from this section, it is lightening fast. When I then read from a different section, it is slow for the first time, but then lightening fast the second time.</p>
  
  <p>I have no idea why this is so.</p>
  
  <p>Does anybody have any more ideas for me?
  Thank you.</p>