# SparseMatrix or SArray, MArray?

**URL:** <https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401>\
**Category:** New to Julia\
**Tags:** array\
**Created:** [May 18, 2021, 10:26pm UTC](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401 "2021-05-18T22:26:16Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 19, 2021, 11:33am UTC](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401/5 "2021-05-19T11:33:54Z")

</div>

For convenience, yes, but this is what I mean:

```julia
using StaticArrays, BenchmarkTools, Setfield

function mut!(A)
  A .= A .+ 1
end
Base.setindex!(m::SMatrix,value,i,j) = @set! m[i,j] = value

A1 = rand(3,3)

A2 = rand(MMatrix{3,3,Float64})

A3 = rand(SMatrix{3,3,Float64})

@btime mut!($A1)

@btime mut!($A2)

@btime mut!($A3)

```

Results:

```julia
  14.548 ns (0 allocations: 0 bytes)
  4.733 ns (0 allocations: 0 bytes)
  1.791 ns (0 allocations: 0 bytes)

```

This actually makes me wonder how and why `MMatrix` are actually implemented as they are, and if that could not be improved.

(it has not escaped to me that the function above does not _mutate_ the matrix if it is a static matrix, and one may have to appeal to `MMatrix` if passing it to another code over which one does not have access to define `A = mut!(A)`) .

---

_[View the full topic](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401)._
