Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
737 views
in Technique[技术] by (71.8m points)

benchmarking - Julia @btime cannot find internal function

I'm wondering if I found a bug in Julia's BenchmarkTools or if there's something deeper happening here that I don't understand. Running the following script

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

works exactly as expected and times both functions. However, using @btime instead of @time gives me an undefined error:

ERROR: UndefVarError: func1 not defined

If I move the internal functions outside test(), both timing versions work fine, but in my actual tests this is not something I can easily do. I prefer using @btime to @time, as it's more accurate and robust, but here I clearly can't. Can someone explain if this is a bug or what's going on here?

question from:https://stackoverflow.com/questions/65836984/julia-btime-cannot-find-internal-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try adding $ to your @btime calls:

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

This interpolates the function definition and now the inner function will be visible to the benchmark.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...