Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between len() and sys.getsizeof() methods in python?

When I ran the below code I got 3 and 36 as the answers respectively.

x ="abd" print len(x) print sys.getsizeof(x) 

Can someone explain to me what's the difference between them ?

like image 263
Balamurugan Avatar asked Jul 10 '13 15:07

Balamurugan


People also ask

What is SYS Getsizeof in Python?

Specifically, the sys. getsizeof() function includes the garbage collector overhead if any: getsizeof() calls the object's __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector.

What does __ sizeof __ do in Python?

Now let's look at the __sizeof__() method. It returns the size of the object without any overhead.

How do you get the length of an integer in Python?

To get the length of an integer in Python:Use the str() class to convert the integer to a string, e.g. result = str(my_int) . Pass the string to the len() function, e.g. len(my_str) . The len() function will return the length of the string.


2 Answers

They are not the same thing at all.

len() queries for the number of items contained in a container. For a string that's the number of characters:

Return the length (the number of items) of an object. The argument may be a sequence (string, tuple or list) or a mapping (dictionary).

sys.getsizeof() on the other hand returns the memory size of the object:

Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.

Python string objects are not simple sequences of characters, 1 byte per character.

Specifically, the sys.getsizeof() function includes the garbage collector overhead if any:

getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector.

String objects do not need to be tracked (they cannot create circular references), but string objects do need more memory than just the bytes per character. In Python 2, __sizeof__ method returns (in C code):

Py_ssize_t res; res = PyStringObject_SIZE + PyString_GET_SIZE(v) * Py_TYPE(v)->tp_itemsize; return PyInt_FromSsize_t(res); 

where PyStringObject_SIZE is the C struct header size for the type, PyString_GET_SIZE basically is the same as len() and Py_TYPE(v)->tp_itemsize is the per-character size. In Python 2.7, for byte strings, the size per character is 1, but it's PyStringObject_SIZE that is confusing you; on my Mac that size is 37 bytes:

>>> sys.getsizeof('') 37 

For unicode strings the per-character size goes up to 2 or 4 (depending on compilation options). On Python 3.3 and newer, Unicode strings take up between 1 and 4 bytes per character, depending on the contents of the string.

like image 103
Martijn Pieters Avatar answered Sep 28 '22 21:09

Martijn Pieters


key difference is that len() will give actual length of elements in container , Whereas sys.sizeof() will give it's memory size which it occupy

for more information read docs of python which is available at https://docs.python.org/3/library/sys.html#module-sys

like image 34
sedmo Avatar answered Sep 28 '22 22:09

sedmo