Skip to content

Instantly share code, notes, and snippets.

@jindraivanek
Created February 16, 2023 06:34
Show Gist options
  • Save jindraivanek/d977e36a9e92051e15c49391f26d041f to your computer and use it in GitHub Desktop.
Save jindraivanek/d977e36a9e92051e15c49391f26d041f to your computer and use it in GitHub Desktop.
module Bench
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
let containsByExists x xs = List.exists ((=) x) xs
let inline containsByExistsInlined x xs = List.exists ((=) x) xs
[<MemoryDiagnoser>]
type ListTests() =
let listOfInts = [ 1..1000 ]
[<Benchmark>]
member _.IntListExists() =
for i in 1..1000 do
List.exists (fun item -> item = i) listOfInts |> ignore
[<Benchmark>]
member _.IntListContains() =
for i in 1..1000 do
List.contains i listOfInts |> ignore
[<Benchmark>]
member _.IntListContainsByExists() =
for i in 1..1000 do
containsByExists i listOfInts |> ignore
[<Benchmark>]
member _.IntListContainsByExistsInlined() =
for i in 1..1000 do
containsByExistsInlined i listOfInts |> ignore
BenchmarkRunner.Run<ListTests>()
@jindraivanek
Copy link
Author

Method Mean Error StdDev Gen0 Allocated
IntListExists 1,011.1 us 20.86 us 60.85 us 3.9063 23.44 KB
IntListContains 23,495.2 us 464.82 us 949.51 us 5093.7500 23460.96 KB
IntListContainsByExists 23,101.4 us 458.04 us 904.13 us 5093.7500 23484.39 KB
IntListContainsByExistsInlined 987.9 us 19.71 us 55.60 us 4.8828 23.44 KB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment