It appears that both of those macros measure both stack and heap allocations, and from a performance point of view I would prefer to focus on just heap allocations. In fact the @btime macro in BenchmarkTools seems to report just heap allocations, but it is quite a complicated macro and I would like something with the simplicity of @allocated akin to “@heapallocated” that would just return the number of heap allocated bytes.
Focusing on the heap seems especially relevant since v1.5 where @view became a stack allocation:
Here is an MWE of the kind of code where I would like to measure heap allocations (to make sure they are zero):
using BenchmarkTools
function f1(x)
v = @view x[:,2] # this should allocate on stack, not heap
v[1]
end
x = zeros(2^20,2^3)
@show @allocated f1(x) # large the first time - compiling
@show @allocated f1(x) # 16 bytes - for the "@view" (on stack)
@show @ballocated f1(x) # 16 again
@time f1(x) # 16 again
@btime f1($x) # 0 bytes !
Clearly the capability is there in @btime but it comes along with a lot of other stuff (and runs longer) so there must be a simpler way?