# Slow multiplication of transpose of sparse matrix

**URL:** <https://discourse.julialang.org/t/slow-multiplication-of-transpose-of-sparse-matrix/80153>\
**Category:** Performance\
**Created:** [April 27, 2022, 5:00pm UTC](https://discourse.julialang.org/t/slow-multiplication-of-transpose-of-sparse-matrix/80153 "2022-04-27T17:00:08Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 27, 2022, 7:05pm UTC](https://discourse.julialang.org/t/slow-multiplication-of-transpose-of-sparse-matrix/80153/6 "2022-04-27T19:05:49Z")

</div>

> [@andresdrenik](#):
>
> In my code the matrix A is
> 
> > 501×501 SparseMatrixCSC{Float64, Integer} with 1501 stored entries:

`Integer` is an abstract type, so working with this is going to be slow. How did you construct this? (See the [performance tip: Avoid containers with abstract types](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container).)

If you use concrete types it should be way faster, with or without the transpose:

```julia
julia> using BenchmarkTools, SparseArrays

julia> A = sprand(501,501,0.006); println(summary(A))
501×501 SparseMatrixCSC{Float64, Int64} with 1529 stored entries

julia> x = rand(501); # note: no need for 501,1 — Julia has 1d arrays

julia> @btime $A * $x;
  2.543 μs (1 allocation: 4.06 KiB)

julia> @btime $(transpose(A)) * $x;
  2.626 μs (1 allocation: 4.06 KiB)

julia> @btime $A' * $x; # shorter transpose for real A
  2.565 μs (1 allocation: 4.06 KiB)

```

PS. Please [quote your code blocks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) with triple backticks.

---

_[View the full topic](https://discourse.julialang.org/t/slow-multiplication-of-transpose-of-sparse-matrix/80153)._
