Skip to content

Instantly share code, notes, and snippets.

@eiriktsarpalis
Last active April 7, 2017 18:06
Show Gist options
  • Save eiriktsarpalis/17b5652d57974d0e3e5d to your computer and use it in GitHub Desktop.
Save eiriktsarpalis/17b5652d57974d0e3e5d to your computer and use it in GitHub Desktop.
Async performance bug
let run x = Async.RunSynchronously x
let runParallel (workflow : Async<'T>) =
[| for i in 1 .. 100 -> workflow |]
|> Async.Parallel
|> run
let a = async { return 42 }
let b = async { return run (async { return 42 })}
#time "on"
run a // Real: 00:00:00.000, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
run b // Real: 00:00:00.000, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
runParallel a // Real: 00:00:00.031, CPU: 00:00:00.015, GC gen0: 0, gen1: 0, gen2: 0
runParallel b // Real: 00:01:24.086, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
// UPDATE: seeing the same behaviour when using inside tasks
open System.Threading.Tasks
// Real: 00:01:21.493, CPU: 00:00:00.062, GC gen0: 2, gen1: 2, gen2: 1
[|for i in 1 .. 100 -> Task.Factory.StartNew(fun () -> run(async { return i})) :> Task |]
|> Task.WaitAll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment