Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stopwatch: measure function execution time #470

Open
natalie-o-perret opened this issue Oct 19, 2021 · 7 comments
Open

Stopwatch: measure function execution time #470

natalie-o-perret opened this issue Oct 19, 2021 · 7 comments

Comments

@natalie-o-perret
Copy link

I discussed this idea with @gusty once on the fssf Slack, but just wanted to know if this could be relevant for the scope of FSharpPlus.

Lss,

[<RequireQualifiedAccess>]
module Stopwatch

open System.Diagnostics


let measure f =
    let stopwatch = Stopwatch.StartNew()
    let r = f()
    stopwatch.Stop()
    (r, stopwatch.Elapsed)

It's a simple helper, so not sure it is worthwhile to add it to the library

@gusty
Copy link
Member

gusty commented Oct 19, 2021

I am not sure, I have the feeling if there are other functions like this we can definitely add them.
What do you think @wallymathieu @adz ?

@adz
Copy link
Contributor

adz commented Oct 19, 2021

I was not aware of this class, but have made similar functions just using current time. I think it seems good.

The only other Stopwatch functionality amounts to being able to inspect seemingly constant IsHighResolution and Frequency (why are they instance fields then?) and to be able to continue timers.

I feel like these aren't generally useful, and the measurefunction is what you'd normally want.

@gusty
Copy link
Member

gusty commented Oct 19, 2021

Thanks, in terms of module and namespaces, what do you think it would be the best option for this and possible other related functions?

@natalie-o-perret
Copy link
Author

natalie-o-perret commented Oct 20, 2021

The idea kinda stemmed from Matlab tic toc functions: https://www.mathworks.com/help/matlab/ref/tic.html

[<AbstractClass; Sealed>]
type private TicToc private () =
    
    static let Stopwatch = Stopwatch()
    
    static member Tic = Stopwatch.Restart
    static member Toc = Stopwatch.Elapsed

let tic = TicToc.Tic
let toc() = TicToc.Toc

This is a simplified version which doesn't support nested scope (unlike Matlab), an alternative impl. could be.

[<AbstractClass; Sealed>]
type private TicToc private () =
    
    static let Stopwatches = Queue<Stopwatch>()
    
    static member Tic = Stopwatch.StartNew >> Stopwatches.Value.Enqueue
    static member Toc = Stopwatches.Dequeue().Elapsed

let tic() = TicToc.Tic()
let toc() = TicToc.Toc

@wallymathieu
Copy link
Member

Seems like a nice thing to have. I can see how this small utility method could fit in with scripting.

@natalie-o-perret
Copy link
Author

Seems like a nice thing to have. I can see how this small utility method could fit in with scripting.

Agreed, in a scripting context, this can really come in handy to have shorthands like that.

Actually I'm wondering if there should be a scripting namespace 🤔

@cannorin
Copy link
Member

something like FSharpPlus.Scripting sounds good?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants