5 people like it.

Timed CEs for both synchronous and asynchronous workflows

Convenient CEs that allow you to measure how long it takes to execute a given content.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
open System
open System.Diagnostics
open System.Threading

type Timed<'T> = { Output: 'T; Elapsed: TimeSpan }
        
type TimedBuilder() =
    member x.Return(value) =
        { Output = value; Elapsed = TimeSpan.Zero }

    member x.Delay(f) =
        let stopwatch = Stopwatch.StartNew()
        let timed = f()
        stopwatch.Stop()
        { timed with Elapsed = timed.Elapsed + stopwatch.Elapsed }

type AsyncTimedBuilder() =
    member b.Zero()                                = async.Zero()
    member b.Delay(generator)                      =
        async {
            let stopwatch = Stopwatch.StartNew()
            let! timed = async.Delay(generator)
            stopwatch.Stop()
            return { timed with Elapsed = timed.Elapsed + stopwatch.Elapsed }
        }
    member b.Return(value)                         =
        async {
            let! timed = async.Return(value)
            return { Output = timed; Elapsed = TimeSpan.Zero }
        }
        
    member b.ReturnFrom(computation: Async<_>)     = async.Return(computation)
    member b.Bind(computation, binder)             = async.Bind(computation, binder)
    member b.Using(resource, binder)               = async.Using(resource, binder)              
    member b.While(guard, computation)             = async.While(guard, computation)     
    member b.For(sequence, body)                   = async.For(sequence, body)         
    member b.Combine(computation1, computation2)   = async.Combine(computation1, computation2)
    member b.TryFinally(computation, compensation) = async.TryFinally(computation, compensation)
    member b.TryWith(computation, catchHandler)    = async.TryWith(computation, catchHandler)

let timed = TimedBuilder()
let asyncTimed = AsyncTimedBuilder()


[<EntryPoint>]
let main _ =
    timed {
        let a = 43
        Thread.Sleep(2000)
        return a
    } |> printfn "%A"
    
    asyncTimed {
        Thread.Sleep(2000)
        let! _ = Async.Sleep(2500)
        let! _ = Async.Sleep(500)         
        return ()
    }
    |> Async.RunSynchronously
    |> printfn "%A"
    
    0
namespace System
namespace System.Diagnostics
namespace System.Threading
type Timed<'T> =
  { Output: 'T
    Elapsed: TimeSpan }
Timed.Output: 'T
Timed.Elapsed: TimeSpan
Multiple items
type TimeSpan =
  struct
    new : ticks:int64 -> TimeSpan + 3 overloads
    member Add : ts:TimeSpan -> TimeSpan
    member CompareTo : value:obj -> int + 1 overload
    member Days : int
    member Divide : divisor:float -> TimeSpan + 1 overload
    member Duration : unit -> TimeSpan
    member Equals : value:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member Hours : int
    member Milliseconds : int
    ...
  end

--------------------
TimeSpan ()
TimeSpan(ticks: int64) : TimeSpan
TimeSpan(hours: int, minutes: int, seconds: int) : TimeSpan
TimeSpan(days: int, hours: int, minutes: int, seconds: int) : TimeSpan
TimeSpan(days: int, hours: int, minutes: int, seconds: int, milliseconds: int) : TimeSpan
Multiple items
type TimedBuilder =
  new : unit -> TimedBuilder
  member Delay : f:(unit -> Timed<'a>) -> Timed<'a>
  member Return : value:'b -> Timed<'b>

--------------------
new : unit -> TimedBuilder
val x : TimedBuilder
val value : 'b
field TimeSpan.Zero: TimeSpan
val f : (unit -> Timed<'a>)
val stopwatch : Stopwatch
Multiple items
type Stopwatch =
  new : unit -> Stopwatch
  member Elapsed : TimeSpan
  member ElapsedMilliseconds : int64
  member ElapsedTicks : int64
  member IsRunning : bool
  member Reset : unit -> unit
  member Restart : unit -> unit
  member Start : unit -> unit
  member Stop : unit -> unit
  static val Frequency : int64
  ...

--------------------
Stopwatch() : Stopwatch
Stopwatch.StartNew() : Stopwatch
val timed : Timed<'a>
Multiple items
type AsyncTimedBuilder =
  new : unit -> AsyncTimedBuilder
  member Bind : computation:Async<'g> * binder:('g -> Async<'h>) -> Async<'h>
  member Combine : computation1:Async<unit> * computation2:Async<'c> -> Async<'c>
  member Delay : generator:(unit -> Async<Timed<'k>>) -> Async<Timed<'k>>
  member For : sequence:seq<'d> * body:('d -> Async<unit>) -> Async<unit>
  member Return : value:'j -> Async<Timed<'j>>
  member ReturnFrom : computation:Async<'i> -> Async<Async<'i>>
  member TryFinally : computation:Async<'b> * compensation:(unit -> unit) -> Async<'b>
  member TryWith : computation:Async<'a> * catchHandler:(exn -> Async<'a>) -> Async<'a>
  member Using : resource:'e * binder:('e -> Async<'f>) -> Async<'f> (requires 'e :> IDisposable)
  ...

--------------------
new : unit -> AsyncTimedBuilder
val b : AsyncTimedBuilder
val async : AsyncBuilder
val generator : (unit -> Async<Timed<'k>>)
val timed : Timed<'k>
val value : 'j
val timed : 'j
val computation : Async<'i>
Multiple items
type Async =
  static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
  static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
  static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
  static member AwaitTask : task:Task -> Async<unit>
  static member AwaitTask : task:Task<'T> -> Async<'T>
  static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
  static member CancelDefaultToken : unit -> unit
  static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
  static member Choice : computations:seq<Async<'T option>> -> Async<'T option>
  static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
  ...

--------------------
type Async<'T> =
val computation : Async<'g>
val binder : ('g -> Async<'h>)
val resource : #IDisposable
val binder : (#IDisposable -> Async<'f>)
val guard : (unit -> bool)
val computation : Async<unit>
val sequence : seq<'d>
val body : ('d -> Async<unit>)
val computation1 : Async<unit>
val computation2 : Async<'c>
val computation : Async<'b>
val compensation : (unit -> unit)
val computation : Async<'a>
val catchHandler : (exn -> Async<'a>)
val timed : TimedBuilder
val asyncTimed : AsyncTimedBuilder
Multiple items
type EntryPointAttribute =
  inherit Attribute
  new : unit -> EntryPointAttribute

--------------------
new : unit -> EntryPointAttribute
val main : string [] -> int
val a : int
Multiple items
type Thread =
  inherit CriticalFinalizerObject
  new : start:ThreadStart -> Thread + 3 overloads
  member Abort : unit -> unit + 1 overload
  member ApartmentState : ApartmentState with get, set
  member CurrentCulture : CultureInfo with get, set
  member CurrentUICulture : CultureInfo with get, set
  member DisableComObjectEagerCleanup : unit -> unit
  member ExecutionContext : ExecutionContext
  member GetApartmentState : unit -> ApartmentState
  member GetCompressedStack : unit -> CompressedStack
  member GetHashCode : unit -> int
  ...

--------------------
Thread(start: ThreadStart) : Thread
Thread(start: ParameterizedThreadStart) : Thread
Thread(start: ThreadStart, maxStackSize: int) : Thread
Thread(start: ParameterizedThreadStart, maxStackSize: int) : Thread
Thread.Sleep(timeout: TimeSpan) : unit
Thread.Sleep(millisecondsTimeout: int) : unit
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
static member Async.Sleep : millisecondsDueTime:int -> Async<unit>
static member Async.RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/85q
Posted:3 years ago
Author:Natalie Perret
Tags: async , asynchronous , computation expression , timespan , stopwatch , measure