Julia @btime 找不到内部函数
Julia @btime cannot find internal function
我想知道我是否在 Julia 的 BenchmarkTools
中发现了错误,或者这里是否发生了我不明白的更深层次的事情。 运行下面的脚本
function test()
function func1(n)
sum(1:n)
end
function func2(n)
ans = 0
for i = 1:n
ans += i
end
return ans
end
@time func1(100000)
@time func2(100000)
end
完全按预期工作,并且对两个函数进行计时。但是,使用 @btime
而不是 @time
会给我一个未定义的错误:
ERROR: UndefVarError: func1 not defined
如果我将内部函数移到 test()
之外,两个计时版本都可以正常工作,但在我的实际测试中,这不是我可以轻易做到的。我更喜欢使用 @btime
而不是 @time
,因为它更准确、更可靠,但在这里我显然不能。有人可以解释这是错误还是这里发生了什么?
尝试将 $
添加到您的 @btime
通话中:
function test()
function func1(n)
sum(1:n)
end
function func2(n)
ans = 0
for i = 1:n
ans += i
end
return ans
end
@btime $func1(100000)
@btime $func2(100000)
end
这会插入函数定义,现在内部函数将对基准可见。
我想知道我是否在 Julia 的 BenchmarkTools
中发现了错误,或者这里是否发生了我不明白的更深层次的事情。 运行下面的脚本
function test()
function func1(n)
sum(1:n)
end
function func2(n)
ans = 0
for i = 1:n
ans += i
end
return ans
end
@time func1(100000)
@time func2(100000)
end
完全按预期工作,并且对两个函数进行计时。但是,使用 @btime
而不是 @time
会给我一个未定义的错误:
ERROR: UndefVarError: func1 not defined
如果我将内部函数移到 test()
之外,两个计时版本都可以正常工作,但在我的实际测试中,这不是我可以轻易做到的。我更喜欢使用 @btime
而不是 @time
,因为它更准确、更可靠,但在这里我显然不能。有人可以解释这是错误还是这里发生了什么?
尝试将 $
添加到您的 @btime
通话中:
function test()
function func1(n)
sum(1:n)
end
function func2(n)
ans = 0
for i = 1:n
ans += i
end
return ans
end
@btime $func1(100000)
@btime $func2(100000)
end
这会插入函数定义,现在内部函数将对基准可见。