There are two ways one can initialize a NXN sparse matrix, whose entries are to be read from one/multiple text files. Which one is faster ? I need the more efficient one, as N is large, typically 10^6.
I could store the (x,y) indices in arrays x, y, the entries in an array v and declare K = sparse(x,y,value);
I could declare K = spzeros(N)
then read of the (i,j) coordinates and values v and insert them as K[i,j]=v;
as they are being read.
I found no tips about this in Julia’s page on sparse arrays.
Don’t insert values one by one: that will be tremendously inefficient since the storage in the sparse matrix needs to be reallocated over and over again.
Just for completeness, if you need to build/update sparse matrices repeatedly, have a look also at the parent method SparseArray.sparse! (not exported)
EDIT: incidentally, you can even avoid allocating, building and processing your x, y, value arrays if you can generate your matrix nonzeros ordered by column. Then, you can build a SparseMatrixCSC directly by generating its internal colptrs, rowvals and nzval fields efficiently. Not sure if you want to mess with such details, though I think you can gain quite a bit of efficiency this way