SparseArrays.jl icon indicating copy to clipboard operation
SparseArrays.jl copied to clipboard

`sort!` is very slow for SparseMatrixCSC

Open LilithHafner opened this issue 2 years ago • 2 comments

julia> x = sprand(10, .1)
10-element SparseVector{Float64, Int64} with 1 stored entry:
  [3 ]  =  0.850332

julia> sort!(x)
10-element SparseVector{Float64, Int64} with 8 stored entries:
  [3 ]  =  0.0
  [4 ]  =  0.0
  [5 ]  =  0.0
  [6 ]  =  0.0
  [7 ]  =  0.0
  [8 ]  =  0.0
  [9 ]  =  0.0
  [10]  =  0.850332

julia> @btime sort!(copy(x)) setup = (x = sprand(1000, .01));
  96.011 μs (4 allocations: 288 bytes)

julia> @btime sort(x) setup = (x = sprand(1000, .01));
  496.583 ns (8 allocations: 512 bytes)

LilithHafner avatar Nov 28 '22 01:11 LilithHafner

Does #303 fix this?

ViralBShah avatar Apr 17 '23 17:04 ViralBShah

Yes for vectors, no for matrices.

julia> using BenchmarkTools, SparseArrays

julia> for d in [1,2]
           print("dense        "); @btime sort!(x, dims=$d) setup=(x=rand(200,200)) evals=1;
           print("sparse k=.03 "); @btime sort!(x, dims=$d, scratch=Vector{Float64}(undef, 200)) setup=(x=sprand(200,200,.03)) evals=1;
           print("sparse k=.003"); @btime sort!(x, dims=$d, scratch=Vector{Float64}(undef, 200)) setup=(x=sprand(200,200,.003)) evals=1;
       end
dense          575.500 μs (601 allocations: 20.52 KiB)
sparse k=.03   3.472 ms (594 allocations: 198.84 KiB)
sparse k=.003  889.666 μs (202 allocations: 53.84 KiB)
dense          638.000 μs (601 allocations: 20.52 KiB)
sparse k=.03   5.226 ms (597 allocations: 198.97 KiB)
sparse k=.003  931.125 μs (208 allocations: 48.59 KiB)

(See JuliaLang/Julia#49392 for why I need to pass in a scratch array)

LilithHafner avatar Apr 17 '23 20:04 LilithHafner