Let’s benchmark the following three:
julia> function a()
x = Vector{Int}()
x
end
a (generic function with 1 method)
julia> function b()
x::Vector{Int} = []
x
end
b (generic function with 1 method)
julia> function c()
x = Int[]
x
end
c (generic function with 1 method)
julia> @benchmark a()
BenchmarkTools.Trial:
memory estimate: 80 bytes
allocs estimate: 1
--------------
minimum time: 16.800 ns (0.00% GC)
median time: 18.700 ns (0.00% GC)
mean time: 21.590 ns (7.51% GC)
maximum time: 1.045 μs (97.54% GC)
--------------
samples: 10000
evals/sample: 1000
julia> @benchmark b()
BenchmarkTools.Trial:
memory estimate: 160 bytes
allocs estimate: 2
--------------
minimum time: 44.209 ns (0.00% GC)
median time: 46.727 ns (0.00% GC)
mean time: 52.021 ns (7.29% GC)
maximum time: 1.514 μs (94.97% GC)
--------------
samples: 10000
evals/sample: 993
julia> @benchmark c()
BenchmarkTools.Trial:
memory estimate: 80 bytes
allocs estimate: 1
--------------
minimum time: 17.116 ns (0.00% GC)
median time: 19.019 ns (0.00% GC)
mean time: 22.369 ns (8.91% GC)
maximum time: 1.356 μs (97.95% GC)
--------------
samples: 10000
evals/sample: 999
What makes b()
so much slower than a()
? Are a()
and c()
equivalent, or the difference of 2-5% really exists?