Created
July 4, 2018 09:36
-
-
Save manofstick/bd374436a6d40218b2d836db34c494ca to your computer and use it in GitHub Desktop.
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
open Perf | |
type EnumA = A0 = 0 | A1 = 1 | A2 = 2 | A3 = 3 | A4 = 4 | A5 = 5 | A6 = 0x7 | |
let getAnEnum i = match i &&& 7 with 0 -> EnumA.A0 | 1 -> EnumA.A1 | 2 -> EnumA.A2 | 3 -> EnumA.A3 | 4 -> EnumA.A4 | 5 -> EnumA.A5 | _ -> EnumA.A6 | |
type R = { | |
GroupByKey : EnumA | |
OtherData : float | |
} | |
let data = [| | |
let r = System.Random 42 | |
for i = 1 to 10000000 do | |
yield { GroupByKey = getAnEnum (r.Next()); OtherData = 42. } | |
|] | |
printfn "%s" Id.Name | |
let total_time = System.Diagnostics.Stopwatch.StartNew () | |
for i = 1 to 10 do | |
let sw = System.Diagnostics.Stopwatch.StartNew () | |
let grouped = | |
(Map.empty, data) | |
||> Array.fold (fun map r -> | |
map | |
|> Map.tryFind r.GroupByKey | |
|> function | |
| None -> map |> Map.add r.GroupByKey [r] | |
| Some value -> map |> Map.add r.GroupByKey (r::value)) | |
printf " iteration=%dms [" sw.ElapsedMilliseconds | |
grouped |> Map.iter (fun key items -> printf "%A=%d;" key items.Length) | |
printfn "]" | |
printfn "total_time=%dms" total_time.ElapsedMilliseconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment