Slow multiplication of transpose of sparse matrix

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.)

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

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 with triple backticks.

2 Likes