# How to speed up creating a sparse matrix?

**URL:** <https://discourse.julialang.org/t/how-to-speed-up-creating-a-sparse-matrix/38800>\
**Category:** Performance\
**Created:** [May 5, 2020, 11:26am UTC](https://discourse.julialang.org/t/how-to-speed-up-creating-a-sparse-matrix/38800 "2020-05-05T11:26:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lesshaste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lesshaste/32/12302_2.png) [@lesshaste](https://discourse.julialang.org/u/lesshaste)\
**Post date:** [May 5, 2020, 11:26am UTC](https://discourse.julialang.org/t/how-to-speed-up-creating-a-sparse-matrix/38800/1 "2020-05-05T11:26:10Z")

</div>

I am attempting to create a sparse matrix from an array of arrays called S . My code is this:

```julia
function make_matrix(nostates, S)
    M = spzeros(Int32, nostates, nostates)
    convert(SparseMatrixCSC{UInt32, UInt32}, M)
    for colno in tqdm(1:nostates)
        for rowno in 1:length(S)
            if S[rowno][colno] > 0
                M[colno, S[rowno][colno]] += 1
            end
        end
    end
    return M
end

```

S has 4 rows and 179765 columns. This code takes around 10 minutes for some reason. Is there something obvious I can do to see why?

Here is some type information:

typeof(S)  
Array{Array{Int32,1},1}

nostates is 179765

It is therefore doing 719060 operations on the sparse matrix so something must be really slow currently.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 5, 2020, 11:39am UTC](https://discourse.julialang.org/t/how-to-speed-up-creating-a-sparse-matrix/38800/2 "2020-05-05T11:39:06Z")

</div>

Yes, if you think about the way a `SparseMatrixCSC` is stored it becomes clear that inserting new non-zero entries is quite expensive (in general requires moving a lot of data). Doing it almost a million times then becomes very expensive.

The way you do it is by creating three vectors containing all the non zero rows, columns and values respectively and then calling `sparse` on that in the end

```julia
julia> I = [1, 4, 3, 5]; J = [4, 7, 18, 9]; V = [1, 2, -5, 3];

julia> S = sparse(I,J,V)
5×18 SparseMatrixCSC{Int64,Int64} with 4 stored entries:
  [1, 4] = 1
  [4, 7] = 2
  [5, 9] = 3
  [3, 18] = -5

```

As an unrelated remark, you have:

```julia
typeof(S)
Array{Array{Int32,1},1}

```

which means that S is a vector of vectors. You probably want to make that a `Matrix` instead.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [May 5, 2020, 11:43am UTC](https://discourse.julialang.org/t/how-to-speed-up-creating-a-sparse-matrix/38800/3 "2020-05-05T11:43:23Z")

</div>

> [@lesshaste](#):
>
> convert(SparseMatrixCSC{UInt32, UInt32}, M)

Should probably be

```julia
M = convert(SparseMatrixCSC{UInt32, UInt32}, M)

```

(just a side node)
