Spurious allocation?

This is a really simple problem, and I’m not sure if I’m missing anything obvious here, but why is the following operation allocating memory?

B = Matrix{Float64}(10,10^5);
@time for j=1:10^5,i=1:10
    B[i,j] = 1.0
end
0.023271 seconds (994.89 k allocations: 15.181 MB, 7.25% gc time)

Don’t benchmark in global scope.

1 Like

Nevermind, this was a global scope issue…

One should default to BenchmarkTools.jl for timing. I have found that @time, tic(), toc() can, for whatever reason, be quite inaccurate, especially for small (\lesssim 10\mu s) intervals, and, more importantly, it includes compile time.

1 Like

Is there confirmation anywhere that BenchmarkTools does not include compile time? Does it just discard the first run’s time?

I’m not sure exactly how it works (I would assume it somehow explicitly forces the code to be compiled before it is run at all), but yes, it does not include compile time. A few casual experiments with @benchmark should confirm this to your satisfaction.

Also note that the package does not magically solve all issues with benchmarks. Replacing the @time in the code above with @benchmark or @btime will not give the correct benchmark result. It’ll still include global access time. For these usage, see the doc https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#introduction