Skip to content

Instantly share code, notes, and snippets.

@vasily-kirichenko
Last active December 19, 2015 10:19
Show Gist options
  • Save vasily-kirichenko/5939777 to your computer and use it in GitHub Desktop.
Save vasily-kirichenko/5939777 to your computer and use it in GitHub Desktop.
module MapVsDictionary
open FSharpx.TimeMeasurement
open System.Collections.Generic
let keys = [1..500000]
compareTwoRuntimes
10
"Map"
(fun() -> keys |> List.fold (fun m x -> m |> Map.add x x) Map.empty)
"Dictionary"
(fun() -> keys |> List.fold (fun (d: Dictionary<_,_>) x -> d.Add(x, x); d) (Dictionary<_,_>()))
// results:
// Map 994.7ms
// Dictionary 39.6ms
// Ratio: 25.11868687
let m = keys |> Seq.map (fun x -> x, x) |> Map.ofSeq
let d = Dictionary<_,_>()
keys |> Seq.iter (fun x -> d.Add(x, x))
compareTwoRuntimes
10
"Map"
(fun() -> keys |> List.iter (fun x -> m |> Map.find x |> ignore))
"Dictionary"
(fun() -> keys |> List.iter (fun x -> d.Item(x) |> ignore))
// results:
// Map 187.9ms
// Dictionary 12.0ms
// Ratio: 15.65833333
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment