Created
October 21, 2016 12:00
-
-
Save cboudereau/4df8de0c086261e177bf03c677423be8 to your computer and use it in GitHub Desktop.
This file contains 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
#time | |
#r @"..\packages\Hopac\lib\net45\Hopac.Core.dll" | |
#r @"..\packages\Hopac\lib\net45\Hopac.dll" | |
#r @"..\packages\Hopac\lib\net45\Hopac.Platform.dll" | |
open Hopac | |
open Hopac.Infixes | |
let t label f = | |
let sw = System.Diagnostics.Stopwatch.StartNew () | |
let r = f () | |
printfn "[%s] done in %ims" label sw.ElapsedMilliseconds | |
System.GC.Collect() | |
System.GC.WaitForPendingFinalizers () | |
r | |
module ParListHopac = | |
let rec fib n = | |
job { | |
if n < 2L then return n | |
else | |
let! r = [fib (n - 2L);fib (n - 1L)] |> Job.seqCollect | |
return r |> Seq.sum } | |
module ParHopac = | |
let rec fib n = | |
job { | |
if n < 2L then return n | |
else let! (x, y) = fib (n - 2L) <*> fib (n - 1L) | |
return x + y } | |
module ParAsc = | |
let rec fib n = | |
async { | |
if n < 2L then return n | |
else | |
let! x = fib (n - 2L) |> Async.StartChild | |
let! y = fib (n - 1L) | |
let! x = x | |
return x + y } | |
module SeqAsc = | |
let rec fib n = | |
async { | |
if n < 2L then return n | |
else | |
let! x = fib (n - 2L) | |
let! y = fib (n - 1L) | |
return x + y } | |
module SeqAscHop = | |
let fib n = SeqAsc.fib n |> Job.fromAsync | |
module ParHopAsc = | |
let fib n = ParHopac.fib n |> Job.toAsync | |
//~100ms | |
t "ParHopac" <| fun () -> ParHopac.fib 30L |> Hopac.run | |
//~100ms | |
t "ParHopAsc" <| fun () -> ParHopAsc.fib 30L |> Async.RunSynchronously | |
//~410ms | |
t "SeqAscHop" <| fun () -> SeqAscHop.fib 30L |> Hopac.run | |
//~410ms | |
t "SeqAsc" <| fun () -> SeqAsc.fib 30L |> Async.RunSynchronously | |
t "ParListHopac" <| fun () -> ParListHopac.fib 30L |> run | |
t "ParAsc" <| fun () -> ParAsc.fib 30L |> Async.RunSynchronously | |
Thank you. But how can I convert async to hopac without big refactoring or hiding Async builder?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that above the
Job.toAsync
andJob.fromAsync
operations are run only once and take far less time than 1ms. IOW, the above is not measuring the performance of either operation. What you are seeing is differences in the performance of other operations.