JuliaLang/julia — pull request #6789 by tanmaykm (2014-05-09 07:17:53+00:00)
Title: more efficient setindex for sparsematrix

This implements `setindex!(A::SparseMatrixCSC, x::Number, I, J)` to be more efficient.

Before:
````
julia> A = sprand(10000, 10000, 0.0001);

julia> @time A[1:8000, 1:8000] = 10; # set some new indices
elapsed time: 62.222216641 seconds (13574393504 bytes allocated)

julia> @time A[1:8000, 1:8000] = 11; # change existing values
elapsed time: 67.845266004 seconds (16635419192 bytes allocated)

julia> @time A[1:8000, 1:8000] = 0; # zero existing values
elapsed time: 5.030329475 seconds (2560507944 bytes allocated)

julia> @time A[1:8000, 1:8000] = 0; # zero already zero'd values
elapsed time: 0.709701096 seconds (512507944 bytes allocated)

julia> @time A[1:8000, 1:8000] = 10; # set all new indices
elapsed time: 52.267994938 seconds (13038875192 bytes allocated)
````

With this patch:
````
julia> A = sprand(10000, 10000, 0.0001);

julia> @time A[1:8000, 1:8000] = 10; # set some new indices
elapsed time: 1.84697863 seconds (1030174960 bytes allocated)

julia> @time A[1:8000, 1:8000] = 11; # change existing values
elapsed time: 0.792033539 seconds (96 bytes allocated)

julia> @time A[1:8000, 1:8000] = 0; # zero existing values
elapsed time: 5.615637808 seconds (2048139680 bytes allocated)

julia> @time A[1:8000, 1:8000] = 0; # zero already zero'd values
elapsed time: 0.000156263 seconds (96 bytes allocated)

julia> @time A[1:8000, 1:8000] = 10; # set all new indices
elapsed time: 1.726549776 seconds (1024139680 bytes allocated)
````