Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function expand_matrix(ms)
- T = eltype(eltype(ms))
- m2 = Matrix{T}(undef, 0, length(first(ms)))
- for i in eachindex(ms)
- m2 = [m2; ms[i]']
- end
- return m2
- end
- function prealloc_matrix(ms)
- T = eltype(eltype(ms))
- l = length(first(ms))
- rows = Vector{Vector{T}}(undef, 0)
- sizehint!(rows, l)
- for r in ms
- push!(rows, r)
- end
- m2 = Matrix{T}(undef, length(rows), l)
- for i in eachindex(rows)
- m2[i,:] .= rows[i]
- end
- return m2
- end
- julia> @benchmark prealloc_matrix(ms) setup=(ms=[zeros(10) for _ in 1:2000])
- BenchmarkTools.Trial:
- memory estimate: 290.48 KiB
- allocs estimate: 2012
- --------------
- minimum time: 54.812 μs (0.00% GC)
- median time: 60.610 μs (0.00% GC)
- mean time: 72.654 μs (12.13% GC)
- maximum time: 1.297 ms (94.20% GC)
- --------------
- samples: 10000
- evals/sample: 1
- julia> @benchmark expand_matrix(ms) setup=(ms=[zeros(10) for _ in 1:2000])
- BenchmarkTools.Trial:
- memory estimate: 152.96 MiB
- allocs estimate: 7797
- --------------
- minimum time: 22.296 ms (8.96% GC)
- median time: 24.119 ms (9.42% GC)
- mean time: 25.338 ms (13.09% GC)
- maximum time: 80.244 ms (68.21% GC)
- --------------
- samples: 190
- evals/sample: 1
- julia> @benchmark expand_matrix(ms) setup=(ms=[zeros(100) for _ in 1:2000])
- BenchmarkTools.Trial:
- memory estimate: 1.49 GiB
- allocs estimate: 7981
- --------------
- minimum time: 255.960 ms (9.87% GC)
- median time: 262.024 ms (10.06% GC)
- mean time: 271.074 ms (12.67% GC)
- maximum time: 320.406 ms (24.57% GC)
- --------------
- samples: 19
- evals/sample: 1
- julia> @benchmark prealloc_matrix(ms) setup=(ms=[zeros(100) for _ in 1:2000])
- BenchmarkTools.Trial:
- memory estimate: 1.67 MiB
- allocs estimate: 2009
- --------------
- minimum time: 406.528 μs (0.00% GC)
- median time: 435.928 μs (0.00% GC)
- mean time: 496.345 μs (8.97% GC)
- maximum time: 42.289 ms (98.87% GC)
- --------------
- samples: 6744
- evals/sample: 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement