Skip to content

Instantly share code, notes, and snippets.

@delneg
Created December 19, 2020 23:09
Show Gist options
  • Save delneg/8f86d38b3f69381360442cda93d72bca to your computer and use it in GitHub Desktop.
Save delneg/8f86d38b3f69381360442cda93d72bca to your computer and use it in GitHub Desktop.
Small benchmark of differenet immutable sets perfomance
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
open System
open System.Security.Cryptography
open System.Collections.Immutable
open Bogus
type Contact =
{ Id: string
Phone: string
FirstName: string
LastName: string }
let genContacts (num:int) =
Bogus
.Faker<Contact>()
.CustomInstantiator(fun f ->
{
Id = f.UniqueIndex.ToString()
Phone = f.Phone.PhoneNumber()
FirstName = f.Person.FirstName
LastName = f.Person.LastName
})
.Generate(num)
let _contacts = genContacts(100000)
let contacts1, contacts2 = (_contacts.GetRange(0,75000),_contacts.GetRange(45000,55000))
printfn "Gathered %i contacts1, %i contacts2" contacts1.Count contacts2.Count
type ContactsBench() =
[<Benchmark>]
member _.FSSet()=
let intersection = Set.intersect (Set.ofSeq contacts1) (Set.ofSeq contacts2)
printfn "There were %i intersections for F# Set" intersection.Count
[<Benchmark>]
member _.IHSet() =
let c1 = ImmutableHashSet.CreateRange(contacts1)
let c2 = ImmutableHashSet.CreateRange(contacts2)
let intersection = c1.Intersect(c2)
printfn "There were %i intersections for ImmutableHashSet" intersection.Count
[<Benchmark>]
member _.ISSet() =
let c1 = ImmutableSortedSet.CreateRange(contacts1)
let c2 = ImmutableSortedSet.CreateRange(contacts2)
let intersection = c1.Intersect(c2)
printfn "There were %i intersections for ImmutableSortedSet" intersection.Count
[<EntryPoint>]
let main argv =
let sets = BenchmarkRunner.Run<ContactsBench>()
0

BenchmarkDotNet=v0.12.1, OS=macOS Catalina 10.15.4 (19E287) [Darwin 19.4.0] Intel Core i9-9980HK CPU 2.40GHz, 1 CPU, 16 logical and 8 physical cores .NET Core SDK=3.1.301 [Host] : .NET Core 3.1.5 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.27001), X64 RyuJIT DEBUG DefaultJob : .NET Core 3.1.5 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.27001), X64 RyuJIT

Method Mean Error StdDev
FSSet 331.9 ms 5.82 ms 9.73 ms
IHSet 159.1 ms 2.98 ms 4.98 ms
ISSet 107.4 ms 2.07 ms 2.76 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment