title: Many small files or one big file? (Or, Overhead of opening and closing file handles) (C++)
body: <p>I have created an application that does the following:</p>
  
  <ol>
  <li>Make some calculations, <strong>write</strong> calculated data to a file - repeat for 500,000 times (over all, write 500,000 files one after the other) - repeat 2 more times (over all, 1.5 mil files were written).</li>
  <li><strong>Read</strong> data from a file, make some intense calculations with the data from the file - repeat for 1,500,000 iterations (iterate over all the files written in step 1.)</li>
  <li>Repeat step 2 for 200 iterations.</li>
  </ol>
  
  <p>Each file is ~212k, so over all i have ~300Gb of data. It looks like the entire process takes ~40 days on a Core 2 Duo CPU with 2.8 Ghz.</p>
  
  <p>My problem is (as you can probably guess) is the time it takes to complete the entire process. All the calculations are serial (each calculation is dependent on the one before), so i can't parallel this process to different CPUs or PCs. I'm trying to think how to make the process more efficient and I'm pretty sure the most of the overhead goes to file system access (duh...). Every time i access a file i open a handle to it and then close it once i finish reading the data. </p>
  
  <p>One of my ideas to improve the run time was to use one big file of 300Gb (or several big files of 50Gb each), and then I would only use one open file handle and simply seek to each relevant data and read it, but I'm not what is the overhead of opening and closing file handles. can someone shed some light on this?</p>
  
  <p>Another idea i had was to try and group the files to bigger ~100Mb files and then i would read 100Mb each time instead of many 212k reads, but this is much more complicated to implement than the idea above.</p>
  
  <p>Anyway, if anyone can give me some advice on this or have any idea how to improve the run time i would appreciate it!</p>
  
  <p>Thanks.</p>
  
  <p><em>Profiler update:</em></p>
  
  <p>I ran a profiler on the process, it looks like the calculations take 62% of runtime and the file read takes 34%. Meaning that even if i miraculously cut file i/o costs by a factor of 34, I'm still left with 24 days, which is quite an improvement, but still a long time :)</p>