Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Declare 2 Byte Variables

Is there a python module that allows me to store a number in a significantly smaller object than int?

My memory is not big enough for some of my data structures, so I would like to represent numbers using as little space as possible.

The problem is I store the values in a tree-structure:

All integers are either keys or values. Maybe then the actual size won't be reduced much when using smaller variables - I don't know.

like image 794
Radio Controlled Avatar asked Feb 12 '23 19:02

Radio Controlled


2 Answers

as much I understand your last comment regarding numpy, you use a treelike structure.

You always can use an integer array also to store a tree.

For example:

index 0 = root     1                     5
      0   1   2    3   4   5 ....        15
   +-------------------------------------------------------+
   | 10 | 5 | 1 ||      ......                             |
   +-------------------------------------------------------+
          |   |    ^                     ^
          |   +----+                     |
          +------------------------------+

In this example, every entry is stored in three entries of the array and the first entry is the value and the others are the (relative -- multiply by 3) indices of the children.

So you can store any treelike data structure in an array.

There is only the problem, that using the references is a little more complicated to program.

Also when your "pointers" have a different size (for example you need >65k entries -- you will need 32bit integers) you must use the bigger sizes. Of course you also could store the three values in two or even three arrays. Just make 3 arrays, one for the value (eg. only 16bit) and two for the references. In this case, you don't need to do any multiplication by yourself since your index will be the same as the normal array index in these arrays.

And by the way: I also guess, that your win would be low, when you would implement it with numpy and let every entry in your tree be an array itself or use small objects containing just one value, since Python has a significant overhead for any object that is stored in the system. But when you use a small amount of arrays like in the example, every array is an object and the overhead is neglect-able since you store many of your entries in a small amount of arrays.

like image 189
Juergen Avatar answered Feb 14 '23 09:02

Juergen


Have a look at numpy arrays:

>>> import numpy as np
>>> np.array([0, 1, 2, 127, -128], dtype='int8')
array([   0,    1,    2,  127, -128], dtype=int8)

np.int8 for example will give you efficient storage for signed integers up to 2**7 - 1.

Warning: values out of range will "wrap around" without any overflow errors:

>>> np.int8(128)
-128

Of course, there are other dtype-s and which is appropriate for you depends on the range of values you need.

like image 36
wim Avatar answered Feb 14 '23 08:02

wim