测量Julia中经过的CPU时间

eds*_*eds 11 performance time cpu-time julia

许多科学计算语言区分绝对时间(挂钟)和CPU时间(处理器周期).例如,在Matlab中我们有:

>> tic; pause(1); toc
Elapsed time is 1.009068 seconds.

>> start = cputime; pause(1); elapsed = cputime - start
elapsed =
        0
Run Code Online (Sandbox Code Playgroud)

在Mathematica,我们有:

>>In[1]:= AbsoluteTiming[Pause[1]]
>>Out[1]= {1.0010572, Null}

>>In[2]:= Timing[Pause[1]]
>>Out[2]= {0., Null}
Run Code Online (Sandbox Code Playgroud)

当对计算服务器上运行的代码进行基准测试时,这种区别非常有用,其中绝对时序结果可能存在很大差异,具体取决于其他进程同时运行的情况.

茱莉亚标准库通过提供支持表达式的时机tic(),toc(),@time和一些其他的功能/宏都基于time_ns(),测量绝对时间的函数.

>>julia> @time sleep(1)
elapsed time: 1.017056895 seconds (135788 bytes allocated)
Run Code Online (Sandbox Code Playgroud)

我的问题:是否有一种简单的方法可以获得Julia中表达式评估所用的CPU时间?

(旁注:做一些挖掘,似乎Julia计时基于libuvuv_hrtime()功能.在我看来,使用同一个库可能会提供一种方法来访问Julia中经过的CPU时间,但我不是专家.有没有人尝试过这样的东西?)uv_getrusage

eds*_*eds 9

我找不到任何现有的解决方案,所以我在这里整理了一个包含一些简单的CPU计时功能的软件包:https://github.com/schmrlng/CPUTime.jl.该包在并行代码上完全未经测试,并且可能有其他错误,但是如果有其他人想尝试调用它

>> Pkg.clone("https://github.com/schmrlng/CPUTime.jl.git")
Run Code Online (Sandbox Code Playgroud)

julia>提示符应该安装包.


小智 7

朱莉娅确实有命令tic()toc()它的工作就像tictoc在Matlab:

julia> tic(); 7^1000000000; toc()
elapsed time: 0.046563597 seconds
0.046563597
Run Code Online (Sandbox Code Playgroud)

  • 我认为 tic() 和 toc() 在 Julia 版本 1.1.0 及更高版本中不再起作用。还是我错了?除了 CPUTime 包之外,最好的选择是什么? (2认同)