title: Is there a way to make this faster? MemoryStream vs FileStream
body: <p>I am working with iTextSharp, and need to generate hundreds of thousands of RTF documents - the resulting files are between 5KB and 500KB.</p>
  
  <p>I am listing 2 approaches below - the original approach wasn't necessarily slow, but I figured why write and retrieve to/from file to get the output string I need. I saw this other approach using MemoryStream, but it actually slowed things down. I essentially just need the outputted RTF content, so that I can run some filters on that RTF to clean up unnecessary formatting. The queries bringing back the data are very quick instant seeming . To generate a 1000 files (actually 2000 files are created in process) with original approach files takes about 15 minutes, the same with second approach takes about 25-30 minutes. The resulting files that I've run are averaging around 80KB.</p>
  
  <p>Is there something wrong with the second approach? Seems like it should be faster than the first one, not slower.</p>
  
  <p>Original approach:</p>
  
  <pre><code>RtfWriter2.GetInstance(doc, new FileStream(RTFFilePathName, FileMode.Create));
  doc.Open();
  
     //Add Tables and stuff here
  
  doc.Close(); //It saves a file here to (RTFPathFileName)
  
  StreamReader srRTF = new StreamReader(RTFFilePathName);
  string rtfText = srRTF.ReadToEnd();
  srRTF.Close();
  
      //Do additional things with rtfText before writing to my final file
  </code></pre>
  
  <p>New approach, trying to speed it up but this is actually half as fast:</p>
  
  <pre><code>  MemoryStream stream = new MemoryStream();
    RtfWriter2.GetInstance(doc, stream);
    doc.Open();
  
       //Add Tables and stuff here
  
    doc.Close();
  
    string rtfText =
    ASCIIEncoding.ASCII.GetString(stream.GetBuffer());
    stream.Close();
  
  
        //Do additional things with rtfText before writing to my final file
  </code></pre>
  
  <p>The second approach I am trying I found here:
  <a href="https://stackoverflow.com/questions/1479050/itextsharp-how-to-generate-a-rtf-document-in-the-clipboard-instead-of-a-file">iTextSharp - How to generate a RTF document in the ClipBoard instead of a file</a></p>