Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to store Python ints in less than 12 bytes?

Tags:

python

int

Is it possible to make Python use less than 12 bytes for an int?

>>> x=int()
>>> x
0
>>> sys.getsizeof(x)
12

I am not a computer specialist but isn't 12 bytes excessive?

The smallest int I want to store is 0, the largest int 147097614, so I shouldn't really need more than 4 bytes.

(There is probably something I misunderstand here as I couldn't find an answer anywhere on the net. Keep that in mind.)

like image 521
The Unfun Cat Avatar asked Nov 15 '12 07:11

The Unfun Cat


People also ask

How many bytes are ints in Python?

To be safe, Python allocates a fixed number of bytes of space in memory for each variable of a normal integer type, which is known as int in Python. Typically, an integer occupies four bytes, or 32 bits.

What is the limit of int in Python?

These represent numbers in the range -2147483648 through 2147483647. (The range may be larger on machines with a larger natural word size, but not smaller.)

How large can an integer be in Python 3?

Integers in Python 3 are of unlimited size. Python 2 has two integer types - int and long.

Why are Python ints 24 bytes?

Getting the size of an integerTo store the number 0, Python uses 24 bytes. Since storing the number zero, Python needs to use only 1 bit. Note that 1 byte equals 8 bits. Therefore, you can think that Python uses 24 bytes as an overhead for storing an integer object.


2 Answers

In python, ints are objects just like everything else. Because of that, there is a little extra overhead just associated with the fact that you're using an object which has some associated meta-data.

If you're going to use lots of ints, and it makes sense to lay them out in an array-like structure, you should look into numpy. Numpy ndarray objects will have a little overhead associated with them for the various pieces of meta-data that the array objects keep track of, but the actual data is stored as the datatype you specify (e.g. numpy.int32 for a 4-byte integer.)

Thus, if you have:

import numpy as np
a = np.zeros(5000,dtype=np.int32)

The array will take only slightly more than 4*5000 = 20000 bytes of your memory

like image 121
mgilson Avatar answered Oct 16 '22 06:10

mgilson


Size of an integer object includes the overhead of maintaining other object information along with its value. The additional information can include object type, reference count and other implementation-specific details.

If you store many integers and want to optimize the space spent, use the array module, specifically arrays constructed with array.array('i').

like image 37
user4815162342 Avatar answered Oct 16 '22 08:10

user4815162342