测量 F# 中的执行时间

发布于 2024-10-09 17:36:02 字数 82 浏览 0 评论 0原文

请发布在 F# 中显示时间的代码。我注意到您可以通过 F# 与 #time 指令交互来测量它,但我不知道如何从 FSI 执行程序

谢谢

Please post code for displaying time in F#. I noticed that you can measure it from F# interactive with #time directive, but I don't know how to execute program from FSI

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁梦 2024-10-16 17:36:02

我只想使用 .NET Stopwatch 类。

let stopWatch = System.Diagnostics.Stopwatch.StartNew()
...
stopWatch.Stop()
printfn "%f" stopWatch.Elapsed.TotalMilliseconds

I would just use the .NET Stopwatch class.

let stopWatch = System.Diagnostics.Stopwatch.StartNew()
...
stopWatch.Stop()
printfn "%f" stopWatch.Elapsed.TotalMilliseconds
高速公鹿 2024-10-16 17:36:02

来自msdn:

#time 本身可以切换是否显示性能信息。
启用后,F# Interactive 会测量实时、CPU 时间和
每个代码段的垃圾收集信息
解释并执行。

因此,要测试您的函数,您必须打开 F# 交互式控制台并在其中执行您的函数(一种方法是选择您的函数,右键单击并选择“在交互中执行”)
然后在 Interactive 中调用您的函数,例如:

// 首先在交互式控制台或文档中定义您的函数:

let square x = x * x

// in interactive
#time
square 10
#time

您将看到计算花费了多少实时和 CPU 时间以及来自垃圾收集器

From msdn:

By itself, #time toggles whether to display performance information.
When it is enabled, F# Interactive measures real time, CPU time, and
garbage collection information for each section of code that is
interpreted and executed.

So to test you function you have to open F# interactive console and execute your function in it (one way to do it is to select your function, right click and chose Execute in Interactive)
Then make a call to your function in Interactive like that for example:

// define your function first either in interactive console or in your document:

let square x = x * x

// in interactive
#time
square 10
#time

You will see how much of real time and CPU time were spent on computation and a bit of information from garbage collector

少女净妖师 2024-10-16 17:36:02

查看F Sharp 编程维基百科中的计时器函数。它的定义如下:

let duration f = 
    let timer = new System.Diagnostics.Stopwatch()
    timer.Start()
    let returnValue = f()
    printfn "Elapsed Time: %i" timer.ElapsedMilliseconds
    returnValue    

使用时如下:

let sample() = System.Threading.Thread.Sleep(2)

duration ( fun() -> sample() )
// or
duration sample

Check out the timer function in the F Sharp Programming wikibook. It is defined like this:

let duration f = 
    let timer = new System.Diagnostics.Stopwatch()
    timer.Start()
    let returnValue = f()
    printfn "Elapsed Time: %i" timer.ElapsedMilliseconds
    returnValue    

Whilst being used like this:

let sample() = System.Threading.Thread.Sleep(2)

duration ( fun() -> sample() )
// or
duration sample
屋顶上的小猫咪 2024-10-16 17:36:02

您还可以创建自定义计算表达式来隐藏实际的测量逻辑,例如:

timer {
   // your code goes here
}

在此处查看更多示例:https ://fsharpforfunandprofit.com/posts/computation-expressions-bind/

You could also create custom computation expression to hide actual measuring logic, e.g.:

timer {
   // your code goes here
}

See more examples here: https://fsharpforfunandprofit.com/posts/computation-expressions-bind/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文