Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...