是否可以像在 Matlab 中那样在 Julia 中嵌套 tic() 和 toc()?如果不是,解决方法是什么?
Is it possible to have nested tic() and toc() in Julia as in Matlab? If not what is the way around it?
是否可以在 Julia 中调用嵌套的 tic() 和 toc()?
问题是我无法将计时器名称 I select 发送到 toc() 函数。
Matlab 中的类似内容(我引用自 Matlab 帮助)
`
REPS = 1000; minTime = Inf; nsum = 10;
tic; % TIC, pair 1
for i=1:REPS
tStart = tic; % TIC, pair 2
total = 0;
for j=1:nsum
total = total + besselj(j,REPS);
end
tElapsed = toc(tStart); % TOC, pair 2
minTime = min(tElapsed, minTime);
end
averageTime = toc/REPS; % TOC, pair 1 `
看起来可以,如果这不是嵌套 tic
和 toc
的意思,请告诉我。
tic()
sleep(1)
tic()
sleep(1)
toc() # elapsed time: 1 second
sleep(1)
toc() #elapsed time: 3 seconds
此外,如果您想在 Julia 中计时,使用 @time
宏会容易得多,它还会跟踪内存分配。
@time (sleep(1); @time sleep(1);)
# elapsed time: 1 second, 672 bytes allocated
# elapsed time: 2.13 seconds, 6 MB allocated
不幸的是,似乎还没有使用 tic
的 return 值的预定义函数。您可以使用 time_ns
函数编写自己的代码。
function toc(t0)
t1 = time_ns()
t = (t1 - t0) / 1e9
println("elapsed time: ", t, " seconds")
return t
end
请注意,如果您需要为多行计时,计时宏 @time
和 @eleapsed
可以与块参数一起使用。
@time begin
do_something()
do_something_more()
end
但由于 Julia 的运行时语义强烈支持函数和局部变量,通常最好将代码块包装在函数中,而不是 运行 全局范围内。
是否可以在 Julia 中调用嵌套的 tic() 和 toc()? 问题是我无法将计时器名称 I select 发送到 toc() 函数。
Matlab 中的类似内容(我引用自 Matlab 帮助)
`
REPS = 1000; minTime = Inf; nsum = 10;
tic; % TIC, pair 1
for i=1:REPS
tStart = tic; % TIC, pair 2
total = 0;
for j=1:nsum
total = total + besselj(j,REPS);
end
tElapsed = toc(tStart); % TOC, pair 2
minTime = min(tElapsed, minTime);
end
averageTime = toc/REPS; % TOC, pair 1 `
看起来可以,如果这不是嵌套 tic
和 toc
的意思,请告诉我。
tic()
sleep(1)
tic()
sleep(1)
toc() # elapsed time: 1 second
sleep(1)
toc() #elapsed time: 3 seconds
此外,如果您想在 Julia 中计时,使用 @time
宏会容易得多,它还会跟踪内存分配。
@time (sleep(1); @time sleep(1);)
# elapsed time: 1 second, 672 bytes allocated
# elapsed time: 2.13 seconds, 6 MB allocated
不幸的是,似乎还没有使用 tic
的 return 值的预定义函数。您可以使用 time_ns
函数编写自己的代码。
function toc(t0)
t1 = time_ns()
t = (t1 - t0) / 1e9
println("elapsed time: ", t, " seconds")
return t
end
请注意,如果您需要为多行计时,计时宏 @time
和 @eleapsed
可以与块参数一起使用。
@time begin
do_something()
do_something_more()
end
但由于 Julia 的运行时语义强烈支持函数和局部变量,通常最好将代码块包装在函数中,而不是 运行 全局范围内。