Julia 中的秒表功能

Stopwatch function in Julia

在MATLAB中,有一对函数tictoc 可用于启动和停止秒表计时器。 取自 link 的示例:

tic
A = rand(12000, 4400);
B = rand(12000, 4400);
toc
C = A'.*B';
toc

我知道 Julia 中有一个宏 @time 具有类似的功能。

julia> @time [sin(cos(i)) for i in 1:100000];
elapsed time: 0.00721026 seconds (800048 bytes allocated)

Julia中有没有一套类似的函数? @time 宏适用于计时语句 可以写成一行或两行。 对于较长的代码部分, 我更喜欢使用 tic-toc 函数。

我试过的

当我用谷歌搜索 "julia stopwatch" 时, 我找到了一个有用的 link 和四个不相关的 link。

  1. 介绍 Julia/Metaprogramming - 维基教科书,打开... 元编程是指编写 Julia 代码来处理和修改 Julia 代码。 ... @time 宏在开头插入一个 "start the stopwatch" 命令 ...
  2. 我们的隐形秒表宣传片 - YouTube 朱莉娅秒表视频
  3. Julia Larson 在推特上写道:“这个#Mac OSX timer/stopwatch 是...
  4. 用秒表为《法国大厨》的剧集计时
  5. 茱莉亚·格里菲斯 | Oiselle 运行 女装

我不知道为什么我没想过只尝试 tic()toc()

来自search of the Julia documentation

tic()

Set a timer to be read by the next call to toc() or toq(). The macro call @time expr can also be used to time evaluation.

tic()toc() 已从 https://github.com/JuliaLang/julia/commit/1b023388f49e13e7a42a899c12602d0fd5d60b0a

开始弃用

您可以将 @elapsed@time 用于较长的块,方法是将它们包装在一个环境中,如下所示:

t = @elapsed begin
    ...
end

还有TickTock.jl,它重新实现了tic()toc()tick()tock()

using TickTock
tick()
# Started timer at 2017-12-13T22:30:59.632
tock()
# 55.052638936 ms: 55 seconds, 52 milliseconds

您也可以使用 time() 获得类似的输出。例如:

t = time()

# code block here
# ...
# ...

dt = time() - t