Why does numpy.zeros takes up little space

你。 提交于 2019-12-28 02:12:07

问题


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

x = numpy.zeros(200000000)

This takes up no memory while,

x = numpy.repeat(0,200000000)

takes up around 1.5GB. Does numpy.zeros create an array of empty pointers? If so, is there a way to set the pointer back to empty in the array after changing it in cython? If I use:

x = numpy.zeros(200000000)
x[0:200000000] = 0.0

The memory usage goes way up. Is there a way to change a value, and then change it back to the format numpy.zeros originally had it in python or cython?


回答1:


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).



来源:https://stackoverflow.com/questions/27574881/why-does-numpy-zeros-takes-up-little-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!