title: C++ write to csv, performance
body: <p>This is probably a simple question, but I have not been able to find specific information on this, or atleast information in a readable format. Most of the information I have found relates to reading data from a .csv.</p>
  
  <p>I am have a function that have to save data to a .csv file. This is not an ideal format in a performance perspective, but let us assume that this cannot change. My data is stored in a <strong>r x c x s</strong> data structure and has to be outputed in the form <strong>r,c,s,value</strong> and saved to the .csv. At the moment i have:</p>
  
  <pre><code>char delimiter = ',';
  ofstream ofs(file, ofstream::out);
  
  for (int r = 0; r &lt; P.n_rows; r++)
  {
      for (int c = 0; c &lt; P.n_cols; c++)
      {
          for (int s = 0; s &lt; P.n_slices; s++)
          {
              ofs &lt;&lt; r + 1 &lt;&lt; delimiter &lt;&lt; c + 1 &lt;&lt; delimiter &lt;&lt; s + 1 &lt;&lt; delimiter &lt;&lt; P(c, s, s) &lt;&lt; endl;
              count++;
          }
      }
  }
  ofs.close();
  </code></pre>
  
  <p>For a data structure of size 100 x 100 x 50 this take roughly 6 sec, which I fell is an necessary long time. I would much appreciate if you could provide some information on how to speed this up.</p>