-
-
Save stjordanis/a6fa99ebc88125c58763a918ff67bb4c 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 BenchmarkDotNet.Attributes | |
open BenchmarkDotNet.Running | |
open System | |
open ExtCore.Control | |
[<MemoryDiagnoser>] | |
type Test() = | |
let choiceFun x y = x |> Choice.bind (fun _ -> y) | |
let resultFun x y = x |> Result.bind (fun _ -> y) | |
[<Benchmark(Baseline = true)>] | |
member __.Choice() : unit = | |
choice { | |
let! x = choiceFun (Choice1Of2 0) (Choice1Of2 0) | |
let! y = choiceFun (Choice1Of2 1) (Choice1Of2 1) | |
let! z = choiceFun (Choice1Of2 2) (Choice1Of2 2) | |
let! h = choiceFun (Choice1Of2 3) (Choice1Of2 3) | |
return! Choice1Of2 (x + y + z + h) | |
} |> ignore | |
[<Benchmark>] | |
member __.Result() : unit = | |
result { | |
let! x = resultFun (Ok 0) (Ok 0) | |
let! y = resultFun (Ok 1) (Ok 1) | |
let! z = resultFun (Ok 2) (Ok 2) | |
let! h = resultFun (Ok 3) (Ok 3) | |
return! Ok (x + y + z + h) | |
} |> ignore | |
[<EntryPoint>] | |
let main _ = | |
printfn "%O" (BenchmarkRunner.Run<Test>()) | |
Console.ReadKey() |> ignore | |
0 | |
## Windows | |
BenchmarkDotNet=v0.10.9, OS=Windows 10 Redstone 2 (10.0.15063) | |
Processor=Intel Core i7-4790K CPU 4.00GHz (Haswell), ProcessorCount=8 | |
Frequency=3906254 Hz, Resolution=255.9997 ns, Timer=TSC | |
.NET Core SDK=2.0.0 | |
[Host] : .NET Core 2.0.0 (Framework 4.6.00001.0), 64bit RyuJIT DEBUG | |
DefaultJob : .NET Core 2.0.0 (Framework 4.6.00001.0), 64bit RyuJIT | |
Method | Mean | Error | StdDev | Scaled | ScaledSD | Gen 0 | Allocated | | |
------- |------------:|----------:|----------:|-------:|---------:|-------:|----------:| | |
Choice | 67.18 ns | 0.0947 ns | 0.0840 ns | 1.00 | 0.00 | 0.0743 | 312 B | | |
Result | 1,039.70 ns | 0.8317 ns | 0.6494 ns | 15.48 | 0.02 | 0.0286 | 128 B | | |
## Ubuntu | |
BenchmarkDotNet=v0.10.9, OS=ubuntu 17.04 | |
Processor=Intel Core i7-4790K CPU 4.00GHz (Haswell), ProcessorCount=8 | |
.NET Core SDK=2.0.0 | |
[Host] : .NET Core 2.0.0 (Framework 4.6.00001.0), 64bit RyuJIT DEBUG | |
DefaultJob : .NET Core 2.0.0 (Framework 4.6.00001.0), 64bit RyuJIT | |
Method | Mean | Error | StdDev | Scaled | Gen 0 | Allocated | | |
------- |---------:|----------:|----------:|-------:|-------:|----------:| | |
Choice | 102.6 ns | 0.0679 ns | 0.0567 ns | 1.00 | 0.0743 | 312 B | | |
Result | 148.7 ns | 0.2098 ns | 0.1859 ns | 1.45 | 0.0303 | 128 B | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment