title: How to optimize time for writing a lot files (saving frames from video)
body: <h2>Background</h2>
  
  <p>I am currently working on a small application that grabs the RGB and depth map streams from a Microsoft Kinect device and saves them on disk for future analysis. Whn I run the program it shall output each frame as a separate image on disk.</p>
  
  <p>The framerate of the Kinect is 30fps, but there are two sources so make this (approximately) 60fps. If I naively try to just save each frame when it arrives I will get dropped frames as is demonstrated by the bundled <a href="https://github.com/OpenKinect/libfreenect/blob/master/fakenect/record.c" rel="nofollow">freenect/record.c</a> application.</p>
  
  <p>I rewrote the application to use one thread that grabs the frames from the device and pushes them to the back of a double ended list (<code>std::deque</code>). Then there are two threads that each pop frames from the front of the double ended list and saves the frames to disk.
  When the recording is turned off, there is a potentially large number of frames left in the list that still need to be recorded, so before exiting we let the two save threads do their work until finished.</p>
  
  <h2>Now the actual problem</h2>
  
  <p>Although the problem of dropped frames is solved, writes to the filesystem are still quite slow. Is there any good way to speed up the file creation on disk?
  Currently, the function <code>dump_frame</code> looks like this:</p>
  
  <pre><code>static void
  dump_frame(struct frame* frame)
  {
      FILE* fp;
      char filename[512]; /* plenty of space! */
  
      sprintf(filename, "d-%f-%u.pgm", get_time, frame-&gt;timestamp);
  
      fp = fopen(filename, "w");
      fprintf(fp, "P5 %d %d 65535\n", frame-&gt;width, frame-&gt;height);
      fwrite(frame-&gt;data, frame-&gt;size, 1, fp);
      fclose(fp);
  }
  </code></pre>
  
  <p>I am running Fedora 14 x64, so the solution only have to concern Linux as operating system.</p>