GithubHelp home page GithubHelp logo

spzeros(T, n) allocates about sparsearrays.jl HOT 3 OPEN

dpinol avatar dpinol commented on September 26, 2024
spzeros(T, n) allocates

from sparsearrays.jl.

Comments (3)

ViralBShah avatar ViralBShah commented on September 26, 2024

It doesn't allocate for nonzero values. It still has to allocate for the SparseMatrixCSC data structure, roughly 1 int per column.

from sparsearrays.jl.

dpinol avatar dpinol commented on September 26, 2024
  • spzeros does not create a SparseMatrixCSC, but a SparseVector. I guess that spzeros allocates because it contains 2 vectors (one for dims and one for values). Even if they're initialized as empty, Julia allocates for empty vectors. See code below
  • In any case, I think the reason why it allocates is not important. What is important is that the documentation says that it does not allocate, and it does. This means that it's misleading when optimized code is required, since the documentation seems to imply that using it and pushing data might have the same performance as setting the data directly through other SparseVector constructors.
function f(n)
           for i in 1:n
               a= Int[]
               resize!(a,1)
           end
       end
f (generic function with 1 method)

julia> @allocations f(10)
20

from sparsearrays.jl.

ViralBShah avatar ViralBShah commented on September 26, 2024

That is right, the empty vectors will be allocated. You are right - the documentation could be improved to not give the wrong impression. If you could make a PR that would be great.

from sparsearrays.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.