Why does numpy.zeros takes up little space

后端 未结 1 1786
抹茶落季
抹茶落季 2020-11-27 22:13

I am wondering why numpy.zeros takes up such little space?

x = numpy.zeros(200000000)

This takes up no memory while,

x = nu         


        
相关标签:
1条回答
  • 2020-11-27 23:05

    Are you using Linux? Linux has lazy allocation of memory. The underlying calls to malloc and calloc in numpy always 'succeed'. No memory is actually allocated until the memory is first accessed.

    The zeros function will use calloc which zeros any allocated memory before it is first accessed. Therfore, numpy need not explicitly zero the array and so the array will be lazily initialised. Whereas, the repeat function cannot rely on calloc to initialise the array. Instead it must use malloc and then copy the repeated to all elements in the array (thus forcing immediate allocation).

    0 讨论(0)
提交回复
热议问题