title: How do I write effectively to a file in F#?
body: <p>I want to generate large xml files for testing purpose but the code I ended up with is really slow, the time grows exponentially with the number of rows I write to the file. Th example below shows that it takes milliseconds to write 100 rows, but it takes over 20 seconds to write 1000 rows (on my machine). I really can't figured out what is making this slow, since I think that writing 1000 rows shouldn't take that long. Also, writing 200 rows takes about 4 times as long as writing 100 rows which is not good. To run the code you might want to change the path for the <code>StreamWriter</code>.</p>
  
  <pre><code>open System.IO
  open System.Diagnostics
  
  let xmlSeq = Seq.initInfinite (fun index -&gt; sprintf "&lt;author&gt;&lt;name&gt;name%d&lt;/name&gt;&lt;age&gt;%d&lt;/age&gt;&lt;books&gt;&lt;book&gt;book%d&lt;/book&gt;&lt;/books&gt;&lt;/author&gt;" index index index)
  
  let createFile (seq: string seq) numberToTake fileName =
      use streamWriter = new StreamWriter("C:\\tmp\\FSharpXmlTest\\FSharpXmlTest\\" + fileName, false)
      streamWriter.WriteLine("&lt;startTag&gt;")
      let rec internalWriter (seq: string seq) (sw:StreamWriter) i (endTag:string) =
          match i with
          | 0 -&gt; (sw.WriteLine(Seq.head seq);
              sw.WriteLine(endTag))
          | _ -&gt; (sw.WriteLine(Seq.head seq);
              internalWriter (Seq.skip 1 seq) sw (i-1) endTag)
      internalWriter seq streamWriter numberToTake "&lt;/startTag&gt;"
  
  let funcTimer fn =
      let stopWatch = Stopwatch.StartNew()
      printfn "Timing started"
      fn()
      stopWatch.Stop()
      printfn "Time elased: %A" stopWatch.Elapsed
  
  
  (funcTimer (fun () -&gt; createFile xmlSeq 100 "file100.xml"))
  (funcTimer (fun () -&gt; createFile xmlSeq 1000 "file1000.xml"))
  </code></pre>