What about F# collections?

time to read 6 min | 1127 words

After disqualifying the BCL immutable collections for performance, I decided that I probably also need to look at the F# collection package, to see if they do any better.

I referenced the FSharp.Core dll and wrote the following:

   1: private static FSharpMap<long, object> FSharpAdd(int iterations)
   2: {
   3:     var dic = MapModule.Empty<long, object>();
   4:  
   5:     var sp = Stopwatch.StartNew();
   6:     var rnd = new Random(32);
   7:     for (int i = 0; i < iterations; i++)
   8:     {
   9:         foreach (var item in Enumerable.Range(rnd.Next(0, i), Math.Max(i * 2, 16)))
  10:         {
  11:             dic = dic.Add(item, null);
  12:         }
  13:     }
  14:  
  15:     Console.WriteLine(sp.Elapsed + " Adding items, fsharp map");
  16:  
  17:     return dic;
  18: }

As I was writing this post, the code is running, and I have had time to do some emails, writing the entire post, check the CPU status, and it is still running. It is likely going to be worse than the immutable collections scenario.

That make sense, since in the immutable collection scenario we had the ability to do many mutations all at once (using SetItems, which isn’t available on the FSharpMap).

However, it clocks in at just over 1 hour and 16 minutes, making it the slowest contender overall.