Skip to content

Instantly share code, notes, and snippets.

@grishace
Last active March 6, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grishace/83f540cb299867e94145551931fcbcb1 to your computer and use it in GitHub Desktop.
Save grishace/83f540cb299867e94145551931fcbcb1 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.WhileTask () =
(task {
let mutable i = 0
while i < this.Length do
i <- i + 1
return i
}).Result
[<Benchmark>]
member this.Rec () =
async {
let mutable i = 0
let rec while' () =
if i = this.Length
then i
else
i <- i + 1
while' ()
return while' ()
} |> Async.RunSynchronously
[<EntryPoint>]
let main _ =
BenchmarkRunner.Run<Test>() |> ignore
0

BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18362
Intel Core i7-4790K CPU 4.00GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.1.102
  [Host]     : .NET Core 3.1.2 (CoreCLR 4.700.20.6602, CoreFX 4.700.20.6702), X64 RyuJIT DEBUG
  Job-LGDGOM : .NET Core 3.1.2 (CoreCLR 4.700.20.6602, CoreFX 4.700.20.6702), X64 RyuJIT

Runtime=.NET Core 3.1  

Method Length Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
WhileAsync 100 4,444.5 ns 87.46 ns 136.16 ns 1.00 0.00 1.8539 - - 7776 B
WhileTask 100 1,369.3 ns 27.08 ns 42.96 ns 0.31 0.02 0.8259 - - 3456 B
Rec 100 403.7 ns 7.97 ns 10.64 ns 0.09 0.00 0.2122 - - 888 B
WhileAsync 1000 40,355.5 ns 786.42 ns 1,127.86 ns 1.00 0.00 15.7471 - - 65880 B
WhileTask 1000 12,482.6 ns 249.24 ns 277.03 ns 0.31 0.01 7.7057 - - 32256 B
Rec 1000 1,804.6 ns 29.19 ns 25.88 ns 0.04 0.00 0.2117 - - 888 B
WhileAsync 10000 411,709.7 ns 7,370.42 ns 6,894.30 ns 1.00 0.00 154.2969 - - 646920 B
WhileTask 10000 128,999.1 ns 2,088.37 ns 1,953.46 ns 0.31 0.01 76.4160 - - 320257 B
Rec 10000 15,223.4 ns 161.78 ns 151.33 ns 0.04 0.00 0.1831 - - 888 B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment