What does allocation mean when creating a new array

Global variables typically cause allocations. The @time macro expands to many variables, so if @time is called in global scope there will be a few extra allocations coming from the macro itself.

julia> @macroexpand @time a = 1+1
quote
    local #9#stats = (Base.gc_num)()
    local #11#elapsedtime = (Base.time_ns)()
    local #10#val = (a = 1 + 1)
    #11#elapsedtime = (Base.time_ns)() - #11#elapsedtime
    local #12#diff = (Base.GC_Diff)((Base.gc_num)(), #9#stats)
    (Base.time_print)(#11#elapsedtime, (#12#diff).allocd, (#12#diff).total_time, (Base.gc_alloc_count)(#12#diff))
    (Base.println)()
    #10#val
end

This is described in the performance tips section in the manual (Performance Tips · The Julia Language):

The 5 allocations seen are from running the @time macro itself in global scope. If we instead run the timing in a function, we can see that indeed no allocations are performed:

Either use BenchmarkTools or make sure @time is not called from global scope. The overhead will be in the hundreds of bytes so for most measurements it doesn’t matter.

1 Like