randn() also has a scalar form.

On Mon, Feb 1, 2016 at 9:56 AM, Erik Schnetter <[email protected]> wrote:

> 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
>
>
> On Mon, Feb 1, 2016 at 9:42 AM, Kristoffer Carlsson
> <[email protected]> wrote:
> >> However  doing in this way is more cumbersome because you need to have a
> >> good estimate of the number of entries
> >
> > Not true. The difference between pre allocating the arrays and just
> pushing
> > into them is not that large due to how julia arrays work (constant
> > ammortized time etc).
> >
> >
> > On Monday, February 1, 2016 at 1:34:12 PM UTC+1, alan souza wrote:
> >>
> >> You could try to use the triplet form (tree vectors containing the
> row/col
> >> indexes and the value of the entry) and call the function sparse.
> >> In this way you can preallocate in advance these three vectors.
> >> However  doing in this way is more cumbersome because you need to have a
> >> good estimate of the number of entries and to explicitly calculate the
> index
> >> for all entries.
> >>
> >> On Sunday, January 31, 2016 at 8:07:56 PM UTC-2, Gabriel Goh wrote:
> >>>
> >>> Generating a sparse matrix from scratch seems to be quite memory
> >>> intensive. and slow. Say I wish to create a large block diagonal
> matrix with
> >>> 2x2 block entries.
> >>>
> >>> Doing it naively is quite slow
> >>>
> >>> function f(k)
> >>>   M = spzeros(2*k,2*k)
> >>>   for i = 1:k
> >>>     D = (i-1)*2 + 1:i*2
> >>>     M[D,D] = randn(2,2)
> >>>   end
> >>>   return M
> >>> end
> >>>
> >>> julia> @time f(10000)
> >>> 2.534277 seconds (239.26 k allocations: 3.013 GB, 15.58% gc time)
> >>>
> >>> Is there a way to speed this up by preallocating the memory somehow?
>
>
>
> --
> Erik Schnetter <[email protected]>
> http://www.perimeterinstitute.ca/personal/eschnetter/
>

Reply via email to