How to use a 5 or 6 bit integer in Python?

Bengt Richter bokr at oz.net
Thu Dec 18 21:58:31 EST 2003


On Fri, 19 Dec 2003 11:42:49 +1100, Glen Wheeler <adsl5lcq at tpg.com.au> wrote:

>
>  Hello all,
>
>  My program uses many millions of integers, and Python is allocating
>way too much memory for these.  I can't have the performance hit by
>using disk, so I figured I'd write a C extension to define a new type.
>  Problem is, my C knowledge is years old and regardless of my
>attempts distutils will not recognise my installation of the MS
>compiler.
>  I am thinking, is there any easier way to use a 5 or 6 bit integer
>in python?  Even a regular 8-bit would be fine.  I only need to
>represent either 32 or 64 distinct numbers.
>
You can store them efficiently in an array, e.g., for unsigned bytes
(or 'b' in place of 'B' for signed):

 >>> import array
 >>> bytes = array.array('B', range(10))
 >>> bytes
 array('B', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 >>> bytes[3]
 3

We can only speculate on further help, until you tell us what you are doing ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list