I am wondering why numpy.zeros takes up such little space?
我想知道为什么麻木。0占用这么小的空间?
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:
占用约1.5 gb。numpy。0创建一个空指针数组?如果是这样,是否有一种方法可以将指针重新设置为在cython中更改后的数组中的空值?如果我使用:
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?
内存使用会大大增加。是否有一种方法可以更改一个值,然后将其更改为格式numpy。0最初在python或cython中有吗?
1 个解决方案
#1
16
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.
你是使用Linux吗?Linux有延迟的内存分配。在numpy中对malloc和calloc的潜在调用总是“成功”。在首次访问内存之前,实际上不会分配内存。
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函数将使用calloc,在第一次访问任何分配的内存之前,它都是0。因此,numpy不需要显式地将数组归零,因此数组将被延迟初始化。然而,repeat函数不能依赖于calloc来初始化数组。相反,它必须使用malloc,然后将重复的内容复制到数组中的所有元素(从而强制立即分配)。
#1
16
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.
你是使用Linux吗?Linux有延迟的内存分配。在numpy中对malloc和calloc的潜在调用总是“成功”。在首次访问内存之前,实际上不会分配内存。
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函数将使用calloc,在第一次访问任何分配的内存之前,它都是0。因此,numpy不需要显式地将数组归零,因此数组将被延迟初始化。然而,repeat函数不能依赖于calloc来初始化数组。相反,它必须使用malloc,然后将重复的内容复制到数组中的所有元素(从而强制立即分配)。