Why does numpy.zeros takes up little space

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

Leave a Comment