Compare this function:
```Julia
function f2(k)
M = spzeros(2*k,2*k)
for i = 1:k
j1 = (i-1)*2+1
j2 = i*2
M[j1,j1] = rand()
M[j2,j1] = rand()
M[j1,j2] = rand()
M[j2,j2] = rand()
end
return M
end
```
which is much faster. It seems your original code has two performance
issues that are unrelated to sparse matrix memory allocation:
(1) `randn` allocates a new matrix every time
(2) Something about indexing sparse matrices with ranges seems slow (I
don't know why)
If you want to continue to use `randn`, then you can use `randn!`
instead, and preallocate the small matrix outside the loop.
-erik
--
Erik Schnetter <
schn...@gmail.com>
http://www.perimeterinstitute.ca/personal/eschnetter/