Clearly view
and reshape
of SparseMatrixCSC
are not performing well. It gets tedious to write, but sometimes you can decompose the reduction into two parts with the first operating on a raw sparse array. If you are always reducing over the 4th dimension, for example, consider reducing over it first and then handle the others after.
Like this
sz4 = (300, 300, 36, 2 * 60 * 12)
sz2 = (sz4[1] * sz4[2] * sz4[3] , sz4[4])
activitygrid = sprand(Float32, sz2..., 111f-6)
# reduce over 4th dim, then 3rd
max34 = dropdims(maximum(reshape(maximum(activitygrid; dims=2), sz4[1:3]); dims=3); dims=3)
# 1.2s on my machine
If you need fancier reductions, consider different storage options, like (dim1, dim2*dim3*dim4)
, that might better reflect your uses. With some post-processing you can usually make these things work, although it sometimes reeks of MATLAB-style index gymnastics.