Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 以F为单位度量执行时间_F#_Time_Measure - Fatal编程技术网

F# 以F为单位度量执行时间

F# 以F为单位度量执行时间,f#,time,measure,F#,Time,Measure,请用F显示时间的代码。我注意到你们可以用F和time指令交互测量时间,但我不知道如何从FSI执行程序 谢谢我只想使用.NET秒表类 let stopWatch = System.Diagnostics.Stopwatch.StartNew() ... stopWatch.Stop() printfn "%f" stopWatch.Elapsed.TotalMilliseconds 查看中的计时器功能。定义如下: let duration f = let timer = new Sys

请用F显示时间的代码。我注意到你们可以用F和time指令交互测量时间,但我不知道如何从FSI执行程序


谢谢

我只想使用.NET秒表类

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

查看中的计时器功能。定义如下:

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
在这样使用时:

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
从msdn:

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

所以要测试您的函数,您必须打开F interactive console并在其中执行您的函数。一种方法是选择您的函数,右键单击并选择execute in interactive 然后在Interactive中调用函数,例如:

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

let square x = x * x

// in interactive
#time
square 10
#time

您将看到有多少实时时间和CPU时间花费在计算上,以及垃圾收集器中的一些信息

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

timer {
   // your code goes here
}

请参阅此处的更多示例:

他们正在使用DateTime。现在,为了在其函数中测量时间,这几乎不能替代秒表计时器。@BrokenGlass wiki在不久前更新为使用秒表。包含在这里供参考,但如果您在FSI F Interactive REPL中,您将获得更详细的CPU时间、实时、GC信息。添加可以从FSI获得完整的分析信息,不需要额外的编程。@Abel你知道PrivateEye在哪里还可用吗?不幸的是,该网站已关闭。我甚至联系了其中一位作者,他告诉我,我仍然可以在某个地方找到代码,但后来联系中断了…:@塞布霍夫,我不知道。我现在只使用Benchmarkdotnet,提供了很多信息,并负责预热时间、平均值、异常值、图表等。它用于改进运行时本身,非常稳定。在撰写本文时,这是不可用的,但不要使用秒表之类的工具,它有极大的缺陷。相反,使用Benchmarkdotnet进行适当的性能测量,并使用一个分析器。