There are some useful additions to the F# tool-belt coming up. Microsoft recently released a preview edition of immutable collections, and if all goes as planned I will release a suite of 6 purely functional linear data structures to FSharpx.Collections in just a couple of days.
Queue is a data structure that needs better representation in the functional world, and both collections make contributions, so this is an opportunity to do some performance comparisons of the queues available to F# programmers. The contenders are two queues from the .NET Framework, System.Collections.Generic.Queue and System.Collections.Concurrent.Queue, the upcoming FSharpx.Collections.Queue, and FSharpx.Datastructures.BatchedQueue, from which FSharpx.Collections.Queue was developed.
The first benchmark exercises something like “real world” usage of the queues with the following steps:
1) enqueue the number of elements indicated
2) peek and dequeue half that number
3) enqueue the same number to the resulting queue
4) peek and dequeue half the resulting size of that queue
5) enqueue one more time the original number
(Milliseconds on a 2.2GHz 4GB dual core 64-bit Windows 7 machine)
And then the obligatory “not so real world” benchmarks
Enqueue the indicated number of elements
Peek and Dequeue until the queue is empty
Initialize from a sequential source (an array unless otherwise indicated)
(The preview Immutable.Queue does not yet implement this capability.)
Use IEnumerable to iterate through each element
(queue of indicated size)
So which Queue is right for You?
That of course depends on your application. For raw speed the system generic queue is hard to beat. The concurrent queue compares well against generic in all categories in a single-threaded environment, so for raw speed it should be a contender in multi-threaded environments, unless composability and immutability is preferred.
Preview Immutable.Queue seems primarily intended for the C# world, but has adequate composability to make it suitable for F#. FSharpx.Collections.Queue performs just as well (sometimes better), and has a broader range of supporting functions including fold, foldback, reverse (usually O(1), worst case O(n)), ofSeq, and ofList.
Pingback: F# Weekly #3, 2013 « Sergey Tihon's Blog
Very nice summary. This will be a useful post to keep handy. Thanks for compiling it.