- Please quote your code so that it’s easy to read: PSA: how to quote code with backticks
- semicolons at the end of each line are not necessary
- When benchmarking code, you will not get accurate results when timing in global scope. That’s why
@StefanKarpinski
asked you earlier. Instead, put the code you’re timing in a function. Running your code in a function, I see3.699408 seconds (41.60 k allocations: 3.787 GiB, 5.39% gc time)
which is already quite close to what you reported MATLAB as giving.
You are seeing a lot of allocations because your code really does allocate a lot of memory. In particular, you are constructing new matrices to hold a lot of intermediate quantities. Modifying your code to pre-allocate those matrices may help a lot. One easy improvement is to broadcast the first line in your loop to avoid allocating a matrix for (sparseR + reshape(q' * sparseS, 199, 199))
and then another one for 0.5 * 0.05 * (sparseR + reshape(q' * sparseS, 199, 199))
:
tmp = 0.5 .* 0.05 .* (sparseR .+ reshape(q' * sparseS, 199, 199))