JuliaSparse / SparseArrays.jl

SparseArrays.jl is a Julia stdlib
https://sparsearrays.juliasparse.org/
Other
90 stars 50 forks source link

Comparing sparse matrices to adjoints is very slow #226

Closed eschnett closed 2 years ago

eschnett commented 2 years ago

Comparing large sparse matrices to their adjoint is very slow. It can be much slower than subtracting them.

julia> A = sparse(Diagonal(ones(10000)));
julia> A==A';
julia> @time A==A';
  0.795819 seconds (1 allocation: 48 bytes)
julia> iszero(A-A');
julia> @time iszero(A-A');
  0.000201 seconds (17 allocations: 625.938 KiB)

I am using

julia> versioninfo()
Julia Version 1.8.0
Commit 5544a0fab76 (2022-08-17 13:38 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin21.4.0)
  CPU: 16 × Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
  Threads: 8 on 16 virtual cores

Naively, I would have expect that A==B is implemented as something like iszero(A-B). Apparently it uses a much slower algorithm.

eschnett commented 2 years ago

Note that expressions such as A == Diagonal(diag(a)) are also much slower than A == sparse(Diagonal(diag(a))).