julia> @benchmark copy.(fill(Int[], 2))
BenchmarkTools.Trial:
memory estimate: 432 bytes
allocs estimate: 5
--------------
minimum time: 100.106 ns (0.00% GC)
median time: 106.006 ns (0.00% GC)
mean time: 130.441 ns (16.75% GC)
maximum time: 30.718 μs (99.35% GC)
--------------
samples: 10000
evals/sample: 949
julia> @benchmark [ Int[] for x in 1:2 ]
BenchmarkTools.Trial:
memory estimate: 256 bytes
allocs estimate: 3
--------------
minimum time: 61.905 ns (0.00% GC)
median time: 64.437 ns (0.00% GC)
mean time: 79.246 ns (17.33% GC)
maximum time: 29.433 μs (99.70% GC)
--------------
samples: 10000
evals/sample: 987
Did I already say, that I love comprehensions?
And we can even force fill and comprehensions:
julia> test=[ fill(1,1) for x in 1:2 ]
2-element Array{Array{Int64,1},1}:
[1]
[1]
julia> push!(test[1], 2)
2-element Array{Int64,1}:
1
2
julia> test
2-element Array{Array{Int64,1},1}:
[1, 2]
[1]
but than its prefilled. But using empty solves that:
julia> test= [ empty(fill(1,1)) for x in 1:2 ]
2-element Array{Array{Int64,1},1}:
[]
[]
julia> push!(test[1], 2)
1-element Array{Int64,1}:
2
julia> test
2-element Array{Array{Int64,1},1}:
[2]
[]
But this is slower than copy.:
julia> @benchmark [ empty(fill(1,1)) for x in 1:2 ]
BenchmarkTools.Trial:
memory estimate: 448 bytes
allocs estimate: 5
--------------
minimum time: 106.249 ns (0.00% GC)
median time: 111.229 ns (0.00% GC)
mean time: 139.833 ns (19.57% GC)
maximum time: 31.456 μs (99.47% GC)
--------------
samples: 10000
evals/sample: 944
Isn’t Julia great again?
But sometimes I think, there are too much possible ways for simple things, but this is another discussion.