Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store boolean values to save memory in python

What is the best way to store between a million to 450,000 Boolean values in a dictionary like collection indexed by a long number? I need to use the least amount of memory possible. True and Int both take up more than 22 bytes per entry. Is there a lower memory per Boolean possible?

like image 916
Martlark Avatar asked Sep 05 '25 01:09

Martlark


2 Answers

Check this question. Bitarray seems to be the preferred choice.

like image 197
Senthess Avatar answered Sep 06 '25 22:09

Senthess


The two main modules for this are bitarray and bitstring (I wrote the latter). Each will do what you need, but some plus and minus points for each:

bitarray

  • Written as a C extension so very quick.
  • Python 2 only.

bitstring

  • Pure Python.
  • Python 2.6+ and Python 3.x
  • Richer array of methods for reading and interpreting data.

So it depends on what you need to do with your data. If it's just storage and retrieval then both will be fine, but for performance critical stuff it's better to use bitarray if you can. Take a look at the docs (bitstring, bitarray) to see which you prefer.

like image 43
Scott Griffiths Avatar answered Sep 06 '25 20:09

Scott Griffiths