How to build this sparsematrix in Julia

Profiling the code:

julia> include("sparse.jl") # Original code
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  127.978 μs …   8.009 ms  ┊ GC (min … max):  0.00% … 91.08%
 Time  (median):     282.130 μs               ┊ GC (median):     0.00%
 Time  (mean ± σ):   330.082 μs ± 556.243 μs  ┊ GC (mean ± σ):  17.04% ±  9.76%

  █ ▆                                                            
  ███▇▄▃▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂▂▁▂▂ ▂
  128 μs           Histogram: frequency by time         4.84 ms <

 Memory estimate: 489.38 KiB, allocs estimate: 1910.

julia> include("sparse.jl") # Improved code
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):   94.017 μs …   1.798 ms  ┊ GC (min … max): 0.00% … 82.30%
 Time  (median):     131.914 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   148.972 μs ± 102.671 μs  ┊ GC (mean ± σ):  5.10% ±  7.64%

  ▇▇▅▅▅██▆▅▄▃▂▂▁ ▁▁▁▂▂▂▂▂▂▂▁▁                                   ▃
  ███████████████████████████████▇▇▇▆▆▆▆▆▁▅▅▅▅▅▆▃▅▅▅▅▅▅▄▅▅▅▁▁▄▄ █
  94 μs         Histogram: log(frequency) by time        508 μs <

 Memory estimate: 377.45 KiB, allocs estimate: 729.

Also is there any particular reason why you are using sparse matrices? For the dimension size you have given us, dense matrices might give a faster result. At the very least for the F matrix, which seems to be a dense matrix instead of a sparse matrix.

That also gives me a considerable speed up in the computation.

1 Like