Skip to content

Instantly share code, notes, and snippets.

@jbtule
Forked from grishace/Program.fs
Last active March 6, 2020 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbtule/12b7137efc5eff33cbbd50cff68da77f to your computer and use it in GitHub Desktop.
Save jbtule/12b7137efc5eff33cbbd50cff68da77f to your computer and use it in GitHub Desktop.
Benchmarking memory traffic from the while loop inside async CE
open System
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
open BenchmarkDotNet.Jobs
open FSharp.Control.Tasks.V2
[<SimpleJob(RuntimeMoniker.NetCoreApp31); MemoryDiagnoser>]
type Test() =
[<Params(100, 1000, 10000)>]
member val Length = 0 with get, set
[<Benchmark(Baseline=true)>]
member this.WhileAsync() =
async {
let mutable i = 0
while i < this.Length do
i <- i + 1
return i
} |> Async.RunSynchronously
[<Benchmark>]
member this.WhileNoAsync() =
async {
let mutable i = 0
let while' () =
while i < this.Length do
i <- i + 1
do while'()
return i
} |> Async.RunSynchronously
[<Benchmark>]
member this.WhileTask () =
(task {
let mutable i = 0
while i < this.Length do
i <- i + 1
return i
}).Result
[<Benchmark>]
member this.RecNoAsync () =
async {
let mutable i = 0
let rec while' () =
if i = this.Length
then i
else
i <- i + 1
while' ()
return while' ()
} |> Async.RunSynchronously
[<Benchmark>]
member this.RecAsync () =
async {
let mutable i = 0
let rec while' () = async{
if i = this.Length
then i
else
i <- i + 1
return! while' ()
}
return! while' ()
} |> Async.RunSynchronously
[<EntryPoint>]
let main _ =
BenchmarkRunner.Run<Test>() |> ignore
0

BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18362
AMD Ryzen 7 1700, 2 CPU, 4 logical and 4 physical cores
.NET Core SDK=3.1.101
  [Host]     : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT DEBUG
  Job-KMAWNL : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT

Runtime=.NET Core 3.1  

Method Length Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
WhileAsync 100 7,323.1 ns 116.16 ns 108.65 ns 1.00 0.00 0.9232 0.0076 - 7776 B
WhileNoAsync 100 758.2 ns 12.61 ns 11.79 ns 0.10 0.00 0.1059 - - 888 B
WhileTask 100 2,189.8 ns 48.89 ns 58.20 ns 0.30 0.01 0.4120 - - 3456 B
RecNoAsync 100 741.1 ns 10.29 ns 9.63 ns 0.10 0.00 0.1059 - - 888 B
RecAsync 100 4,974.8 ns 96.64 ns 85.67 ns 0.68 0.01 1.0605 - - 8920 B
WhileAsync 1000 66,316.9 ns 1,309.51 ns 1,224.91 ns 1.00 0.00 7.8125 0.1221 - 65880 B
WhileNoAsync 1000 3,143.7 ns 60.17 ns 59.10 ns 0.05 0.00 0.1030 - - 888 B
WhileTask 1000 20,019.2 ns 389.63 ns 478.50 ns 0.30 0.01 3.8452 - - 32256 B
RecNoAsync 1000 3,168.0 ns 52.43 ns 49.04 ns 0.05 0.00 0.1030 - - 888 B
RecAsync 1000 45,091.1 ns 824.55 ns 771.28 ns 0.68 0.02 9.6436 - - 81088 B
WhileAsync 10000 644,875.3 ns 9,344.67 ns 8,283.81 ns 1.00 0.00 77.1484 0.9766 - 646920 B
WhileNoAsync 10000 27,566.9 ns 418.43 ns 391.40 ns 0.04 0.00 0.0916 - - 888 B
WhileTask 10000 197,274.1 ns 2,292.18 ns 2,031.96 ns 0.31 0.01 38.0859 - - 320257 B
RecNoAsync 10000 27,269.9 ns 371.37 ns 347.38 ns 0.04 0.00 0.0916 - - 888 B
RecAsync 10000 445,178.9 ns 5,984.27 ns 5,304.90 ns 0.69 0.01 95.7031 - - 802768 B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment