许多科学计算语言区分绝对时间(挂钟时间)和 CPU 时间(处理器周期)。例如,在 Matlab 中,我们有:
>> tic; pause(1); toc
Elapsed time is 1.009068 seconds.
>> start = cputime; pause(1); elapsed = cputime - start
elapsed =
0
在Mathematica中,我们有:
>>In[1]:= AbsoluteTiming[Pause[1]]
>>Out[1]= {1.0010572, Null}
>>In[2]:= Timing[Pause[1]]
>>Out[2]= {0., Null}
当在计算服务器上运行代码时,区分这一点非常有用,因为根据同时运行的其他进程而定,绝对时间结果可能会存在很高的变异性。
Julia标准库通过tic()
, toc()
, @time
和其他一些基于time_ns()
的函数/宏来支持表达式的计时。
>>julia> @time sleep(1)
elapsed time: 1.017056895 seconds (135788 bytes allocated)
我的问题是:在Julia中有没有一种简单的方法来获取表达式求值的经过时间?
(顺便说一句:经过一番搜索,似乎Julia的计时是基于来自libuv库的uv_hrtime()
函数。使用该库中的uv_getrusage
可能会提供一种访问Julia中经过时间的方法,但我不是专家。是否有人尝试过类似的东西?)