Constructing a sparse matrix via the following is slow:
n = 200 m = 250 A = spzeros(n,m) for i in 1:n for j in 1:m A[i,j] = rand() end end
The way to speed this up is to use the following syntax:
cur_I = Int cur_J = Int cur_V = Number for i in 1:n append!(cur_I, repeat([i], m)) for j in 1:m push!(cur_J, j) push!(cur_V, rand()) end end A = sparse(cur_I, cur_J, cur_V, n, m)
This begs the question, why isn’t there a constructor that can lazily put off construction of a sparse matrix until all the elements are ready?
The pseudocode for the result would be something like:
A = spzeros_lazy(n,m) for i in 1:n for j in 1:m A[i,j] = rand() end end lazy_sparse_constructor!(A)
// the goal is not to be the most performant, just performant && easy to code