RomainL.
RomainL.

Reputation: 1014

numpy array bigger than python list

In this question Why NumPy instead of Python lists? [closed] every one seems to agree than numpy array are a more compact structure. I try to replicate this and I found than is only true when the list become huge. I am on python3.5 ubuntu 12.04

import sys
from numpy getsizeof

a = [1.0,2.0,3.0,4.0]
print(getsizeof(a))  # 96
print(getsizeof(numpy.array(a)))  # 128

a = list(range(1000))
print(getsizeof(a))  # 9112
print(getsizeof(numpy.array(a)))  # 8096

Could someone explain me why?

Upvotes: 0

Views: 1120

Answers (1)

cco
cco

Reputation: 6281

Fixed overhead. Both lists and numpy arrays have a fixed-size data structure that is used to manage the data in the container. Numpy has a slightly larger structure, which the more compact value storage doesn't immediately overcome.

Upvotes: 3

Related Questions