How to benchmark append!?

You also need to set evals=1 (see: BenchmarkTools setup isn't run between each iteration? - #4 by rdeits ) otherwise you will get unpredictable behavior due to re-use of the setup value between evaluations.

With that change, using resize is the fastest option, which is what you would expect:

julia> begin
              print("push!  :"); @btime f!(xf) setup = xf=[0] evals = 1
              print("append!:"); @btime g!(xf) setup = xf=[0] evals = 1
              print("new vec:"); @btime h(xf) setup = xf=[0] evals = 1
              print("resize :"); @btime k!(xf) setup = xf=[0] evals = 1
              end
push!  :  520.000 ns (6 allocations: 2.13 KiB)
append!:  106.000 ns (2 allocations: 1.67 KiB)
new vec:  89.000 ns (1 allocation: 896 bytes)
resize :  75.000 ns (1 allocation: 816 bytes)
5 Likes