Skip to content

Commit

Permalink
Add more F# microbenchmarks (#4375)
Browse files Browse the repository at this point in the history
* More F# microbenchmarks

* Update MicrobenchmarksFSharp.fsproj
  • Loading branch information
psfinaki authored Aug 21, 2024
1 parent 30804e7 commit 11fe472
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/benchmarks/micro-fsharp/Equality/Arrays.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module MicroBenchmarks.FSharp.Equality.Arrays

open BenchmarkDotNet.Attributes
open MicroBenchmarks.FSharp

[<MemoryDiagnoser>]
[<BenchmarkCategory(Categories.FSharpMicroCategory)>]
type Arrays() =

let numbers = Array.init 1000 id

[<Benchmark>]
member _.Int32() =
numbers |> Array.countBy (fun n -> [| n % 7 |])

[<Benchmark>]
member _.Int64() =
numbers |> Array.countBy (fun n -> [| int64 (n % 7) |])

[<Benchmark>]
member _.Byte() =
numbers |> Array.countBy (fun n -> [| byte (n % 7) |])

[<Benchmark>]
member _.Obj() =
numbers |> Array.countBy (fun n -> [| box (n % 7) |])
65 changes: 65 additions & 0 deletions src/benchmarks/micro-fsharp/Equality/BasicTypes.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module MicroBenchmarks.FSharp.Equality.BasicTypes

open BenchmarkDotNet.Attributes
open MicroBenchmarks.FSharp

[<MemoryDiagnoser>]
[<BenchmarkCategory(Categories.FSharpMicroCategory)>]
type BasicTypes() =

let bools = Array.init 1000 (fun n -> n % 2 = 0)
let sbytes = Array.init 1000 sbyte
let bytes = Array.init 1000 byte
let int16s = Array.init 1000 int16
let uint16s = Array.init 1000 uint16
let int32s = Array.init 1000 id
let uint32s = Array.init 1000 uint32
let int64s = Array.init 1000 int64
let uint64s = Array.init 1000 uint64
let intptrs = Array.init 1000 nativeint
let uintptrs = Array.init 1000 unativeint
let chars = Array.init 1000 char
let strings = Array.init 1000 string
let decimals = Array.init 1000 decimal

[<Benchmark>]
member _.Bool() = bools |> Array.distinct

[<Benchmark>]
member _.SByte() = sbytes |> Array.distinct

[<Benchmark>]
member _.Byte() = bytes |> Array.distinct

[<Benchmark>]
member _.Int16() = int16s |> Array.distinct

[<Benchmark>]
member _.UInt16() = uint16s |> Array.distinct

[<Benchmark>]
member _.Int32() = int32s |> Array.distinct

[<Benchmark>]
member _.UInt32() = uint32s |> Array.distinct

[<Benchmark>]
member _.Int64() = int64s |> Array.distinct

[<Benchmark>]
member _.UInt64() = uint64s |> Array.distinct

[<Benchmark>]
member _.IntPtr() = intptrs |> Array.distinct

[<Benchmark>]
member _.UIntPtr() = uintptrs |> Array.distinct

[<Benchmark>]
member _.Char() = chars |> Array.distinct

[<Benchmark>]
member _.String() = strings |> Array.distinct

[<Benchmark>]
member _.Decimal() = decimals |> Array.distinct
105 changes: 105 additions & 0 deletions src/benchmarks/micro-fsharp/Equality/FSharpCoreFunctions.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
module MicroBenchmarks.FSharp.Equality.FSharpCoreFunctions

open BenchmarkDotNet.Attributes
open MicroBenchmarks.FSharp

[<Struct>]
type SomeStruct =
val A : int
new a = { A = a }

[<MemoryDiagnoser>]
[<BenchmarkCategory(Categories.FSharpMicroCategory)>]
type FSharpCoreFunctions() =

let array = Array.init 1000 id
let list = List.init 1000 id
let seq = Seq.init 1000 id

[<Benchmark>]
member _.ArrayCountBy() =
array
|> Array.countBy (fun n -> SomeStruct(n % 7))

[<Benchmark>]
member _.ArrayGroupBy() =
array
|> Array.groupBy (fun n -> SomeStruct(n % 7))

[<Benchmark>]
member _.ArrayDistinct() =
array
|> Array.map (fun n -> SomeStruct(n % 7))
|> Array.distinct

[<Benchmark>]
member _.ArrayDistinctBy() =
array
|> Array.distinctBy (fun n -> SomeStruct(n % 7))

[<Benchmark>]
member _.ArrayExcept() =
array
|> Array.map SomeStruct
|> Array.except ([| SomeStruct 42 |])

[<Benchmark>]
member _.ListCountBy() =
list
|> List.countBy (fun n -> SomeStruct(n % 7))

[<Benchmark>]
member _.ListGroupBy() =
list
|> List.groupBy (fun n -> SomeStruct(n % 7))

[<Benchmark>]
member _.ListDistinct() =
list
|> List.map (fun n -> SomeStruct(n % 7))
|> List.distinct

[<Benchmark>]
member _.ListDistinctBy() =
list
|> List.distinctBy (fun n -> SomeStruct(n % 7))

[<Benchmark>]
member _.ListExcept() =
List.init 1000 id
|> List.map SomeStruct
|> List.except ([| SomeStruct 42 |])

[<Benchmark>]
member _.SeqCountBy() =
seq
|> Seq.countBy (fun n -> SomeStruct(n % 7))
|> Seq.last

[<Benchmark>]
member _.SeqGroupBy() =
seq
|> Seq.groupBy (fun n -> SomeStruct(n % 7))
|> Seq.last

[<Benchmark>]
member _.SeqDistinct() =
seq
|> Seq.map (fun n -> SomeStruct(n % 7))
|> Seq.distinct
|> Seq.last

[<Benchmark>]
member _.SeqDistinctBy() =
seq
|> Seq.distinctBy (fun n -> SomeStruct(n % 7))
|> Seq.last

[<Benchmark>]
member _.SeqExcept() =
seq
|> Seq.map SomeStruct
|> Seq.except ([| SomeStruct 42 |])
|> Seq.last


3 changes: 3 additions & 0 deletions src/benchmarks/micro-fsharp/MicrobenchmarksFSharp.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

<ItemGroup>
<Compile Include="Categories.fs" />
<Compile Include="Equality\BasicTypes.fs" />
<Compile Include="Equality\FSharpCoreFunctions.fs" />
<Compile Include="Equality\Arrays.fs" />
<Compile Include="Collections.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
Expand Down

0 comments on commit 11fe472

Please sign in to comment.