Created
July 25, 2015 19:39
-
-
Save manofstick/5709f0e715a247900722 to your computer and use it in GitHub Desktop.
Comparing NonStructural Comparer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Program | |
open System | |
open System.Collections.Generic | |
open System.Diagnostics | |
let r = Random 42 | |
let count = 1000000 | |
let checkValueDate = DateTime (1980, 2, 18) | |
let dobs = [| | |
for i = 0 to count do | |
yield DateTime(r.Next(1973, 2016), r.Next(1,13), r.Next(1,29)) | |
|] | |
let check ``method`` (iec:IEqualityComparer<DateTime>) = | |
let sw = Stopwatch.StartNew () | |
let d = Dictionary iec | |
for dob in dobs do | |
let mutable cell = Unchecked.defaultof<_> | |
match d.TryGetValue (dob, &cell) with | |
| false -> d.[dob] <- 1 | |
| true -> d.[dob] <- d.[dob] + 1 | |
let time = sw.ElapsedMilliseconds | |
let maximum = | |
d | |
|> Seq.maxBy (fun kv ->kv.Value) | |
let checkValue = | |
d | |
|> Seq.filter (fun kv -> kv.Value = maximum.Value) | |
|> Seq.sortBy (fun kv -> kv.Key) | |
|> Seq.head | |
|> fun head -> head.Key | |
if checkValueDate <> checkValue then | |
failwith "unexpected result" | |
printfn "%s time = %d" ``method`` time | |
let dynamicallyGenerated<'key when 'key : comparison> () = | |
match HashIdentity.Structural<'key> with | |
| :? IEqualityComparer<DateTime> as ec -> ec | |
| _ -> failwith "unexpected" | |
[<EntryPoint>] | |
let main _ = | |
let staticallyGeneratedNonStructural = HashIdentity.NonStructural<DateTime> | |
let staticallyGeneratedStructural = HashIdentity.Structural<DateTime> | |
let dynamicallyGeneratedStructural = dynamicallyGenerated<DateTime> () | |
for i = 1 to 10 do | |
check "ns" staticallyGeneratedNonStructural | |
for i = 1 to 10 do | |
check "s" staticallyGeneratedStructural | |
for i = 1 to 10 do | |
check "ds" dynamicallyGeneratedStructural | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment