NoseKnowsAll
NoseKnowsAll

Reputation: 4623

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?

Upvotes: 2

Views: 717

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42194

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.

Upvotes: 3

Related Questions