Altough not as handy as StopWatch you can use Date.Now() to measure time:

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
let rnd = System.Random()

let test () =
    async {
        do! Async.Sleep 1500
        return rnd.Next()
    }

let time f =
    async {
        let s = Date.Now()
        let! res = f ()
        let diff = Date.Now() - s
        printfn "Function took %dms to execute." diff
        return res
    }

let Main =
    time <| fun () ->
        async {
            let! r = test ()
            return r + 12
        }
    |> Async.Ignore
    |> Async.Start

Edit: as Loic said precision is pretty bad with this method.

By on 11/24/2015 5:26 AM ()

Thanks, I think the binding a much better solution :)

By on 11/24/2015 6:05 AM ()

If you just want to print the execution time on the console, your best bet is probably the JavaScript console API, available under WebSharper.JavaScript as Console.Time("label") ... Console.TimeEnd("label").

If you want to use the results in your code, I see there's a performance API for which you'd need to write a small binding. Or you can just use Date from WebSharper.JavaScript, although I think the guaranteed precision is pretty weak.

By on 11/24/2015 5:23 AM ()

Thanks, I've written a binding (+ a little helper function):

1
2
3
4
5
6
7
8
9
[<Direct "performance.now()">]
let now(): int = X<_>

let timeAsync(f: unit -> 'a Async): ('a * int) Async =
    async {
        let start = now()
        let! result = f()
        return result, int (now() - start)
    }
By on 11/24/2015 6:01 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper