davidavdav / NamedArrays.jl

Julia type that implements a drop-in replacement of Array with named dimensions
Other
118 stars 20 forks source link

faster show for sparse matrix #100

Closed tomhaber closed 3 years ago

tomhaber commented 3 years ago

I noticed that show gets pretty slow when you run it on large matrices because it iterates over all nonzero elements. This patch uses binary search to quickly find the columns for the nonzeros. It does require slightly more memory, but it is quite fast.

tomhaber commented 3 years ago

As an example:

using NamedArrays, SparseArrays
X = NamedArray(sprand(500000, 30000, 0.01));

@time show(stdout, X) # 0.235192 seconds (996 allocations: 12.177 MiB)
@time show(IOContext(stdout, :limit => true), X) # 18.834450 seconds (750.02 M allocations: 11.188 GiB, 9.13% gc time)

with the different implementation

@time show(stdout, X) # 0.016669 seconds (1.05 k allocations: 12.180 MiB)
@time show(IOContext(stdout, :limit => true), X) # 0.015760 seconds (1.17 k allocations: 12.183 MiB)
davidavdav commented 3 years ago

Thanks for this work,

There seems to be no more automatic testing in these PRs nowadays. I checked out locally and ran the standard test—i hope this was actually the local package, I somehow never know.