Skip to content

Instantly share code, notes, and snippets.

@tk3369
Created May 22, 2021 23:44
Show Gist options
  • Save tk3369/a933df2a601cf526ac791baec2fec09c to your computer and use it in GitHub Desktop.
Save tk3369/a933df2a601cf526ac791baec2fec09c to your computer and use it in GitHub Desktop.
Performance of converting vector-of-vector to matrix
using BenchmarkTools
g(y) = [[y[j], 2y[j]] for j in 1:length(y)]
f8(x) = reduce(hcat, x)'
function f11(v::Vector{Vector{T}}) where T
Mat = Matrix{T}(undef, length(v), length(v[1]))
@inbounds for row in 1:length(v)
for col in 1:length(v[1])
Mat[row, col] = v[row][col]
end
end
Mat
end
function f23(v::Vector{Vector{T}}) where T
[@inbounds v[row][col] for row in 1:length(v), col in 1:length(v[1])]
end
function bmk(n)
@info "Testing $n"
y = rand(n)
z = g(y)
@btime f8($z)
@btime f11($z)
@btime f23($z)
return nothing
end
bmk(10)
bmk(100)
bmk(1000)
bmk(10000)
#= Results
julia> bmk(10)
[ Info: Testing 10
84.206 ns (1 allocation: 240 bytes)
64.147 ns (1 allocation: 240 bytes)
64.657 ns (1 allocation: 240 bytes)
julia> bmk(100)
[ Info: Testing 100
710.925 ns (1 allocation: 1.77 KiB)
541.139 ns (1 allocation: 1.77 KiB)
341.806 ns (1 allocation: 1.77 KiB)
julia> bmk(1000)
[ Info: Testing 1000
8.599 μs (1 allocation: 15.75 KiB)
7.326 μs (1 allocation: 15.75 KiB)
4.126 μs (1 allocation: 15.75 KiB)
julia> bmk(10000)
[ Info: Testing 10000
81.532 μs (2 allocations: 156.33 KiB)
67.016 μs (2 allocations: 156.33 KiB)
44.960 μs (2 allocations: 156.33 KiB)
=#
# How about Zygote
h8 = (y-> sum(abs2, y)) ∘ f8 ∘ g
h11 = (y-> sum(abs2, y)) ∘ f11 ∘ g
h23 = (y-> sum(abs2, y)) ∘ f23 ∘ g
gradient(h8, y) # OK
gradient(h11, y) # Error: Mutating arrays is not supported
gradient(h23, y) # Error: Need an adjoint for constructor Base.Iterators.ProductIterator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment