怎么利用@benchmark中的@belapsed测试循环中每一次循环所需要的时间,并把时间放到一个数组中
利用如下示例代码会出现,y没有被定义的错误
代码如下:
function foo()
x=rand(10)
y=0
t=zeros(10)
for i=1:10
t[i]=@belapsed begin
y=y+x[i]
end
end
return y,t
end
y,t=foo()
错误提示如下:
y,t=foo()
ERROR: UndefVarError: y not defined
Stacktrace:
[1] macro expansion at .\REPL[56]:6 [inlined]
[2] ##core#264() at C:\Users\你好.julia\packages\BenchmarkTools\ms0Xc\src\execution.jl:479
[3] ##sample#265(::BenchmarkTools.Parameters) at C:\Users\你好.julia\packages\BenchmarkTools\ms0Xc\src\execution.jl:485
[4] _run(::BenchmarkTools.Benchmark, ::BenchmarkTools.Parameters; verbose::Bool, pad::String, kwargs::Base.Iterators.Pairs{Symbol,Integer,NTuple{4,Symbol},NamedTuple{(:samples, :evals, :gctrial, :gcsample),Tuple{Int64,Int64,Bool,Bool}}}) at C:\Users\你好.julia\packages\BenchmarkTools\ms0Xc\src\execution.jl:98
[5] (::Base.var"#inner#2"{Base.Iterators.Pairs{Symbol,Integer,NTuple{5,Symbol},NamedTuple{(:verbose, :samples, :evals, :gctrial, :gcsample),Tuple{Bool,Int64,Int64,Bool,Bool}}},typeof(BenchmarkTools._run),Tuple{BenchmarkTools.Benchmark,BenchmarkTools.Parameters}})() at .\essentials.jl:713
[6] #invokelatest#1 at .\essentials.jl:714 [inlined]
[7] #run_result#38 at C:\Users\你好.julia\packages\BenchmarkTools\ms0Xc\src\execution.jl:33 [inlined]
[8] run(::BenchmarkTools.Benchmark, ::BenchmarkTools.Parameters; progressid::Nothing, nleaves::Float64, ndone::Float64, kwargs::Base.Iterators.Pairs{Symbol,Integer,NTuple{5,Symbol},NamedTuple{(:verbose, :samples, :evals, :gctrial, :gcsample),Tuple{Bool,Int64,Int64,Bool,Bool}}}) at C:\Users\你好.julia\packages\BenchmarkTools\ms0Xc\src\execution.jl:116
[9] #warmup#47 at C:\Users\你好.julia\packages\BenchmarkTools\ms0Xc\src\execution.jl:168 [inlined]
[10] warmup(::BenchmarkTools.Benchmark) at C:\Users\你好.julia\packages\BenchmarkTools\ms0Xc\src\execution.jl:168
[11] macro expansion at C:\Users\你好.julia\packages\BenchmarkTools\ms0Xc\src\execution.jl:387 [inlined]
[12] foo() at .\REPL[56]:5
[13] top-level scope at REPL[57]:1