如何在Python中使用5位或6位整数? [英] How to use a 5 or 6 bit integer in Python?

查看:534
本文介绍了如何在Python中使用5位或6位整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



大家好,

我的程序使用了数百万的整数,而Python正在分配

太多的内存对于这些。使用磁盘我不能用

来打击性能,所以我想我会写一个C扩展来定义一个新类型。

问题是,我的C知识已经存在多年了,不管我的是什么?b $ b尝试distutils都无法识别我安装的MS

编译器。

我在想,是有没有更简单的方法在python中使用5或6位整数

?即使是普通的8位也没关系。我只需要

代表32或64个不同的数字。


谢谢,

Glen

解决方案

2003年12月19日星期五11:42:49 +1100,Glen Wheeler< ad ****** @ tpg.com.au>写道:


大家好,

我的程序使用了数百万的整数,而Python分配的内存太多了这些。使用磁盘我无法获得性能,所以我想我会写一个C扩展来定义一个新类型。
问题是,我的C知识已经存在多年了,而且无论如何我的
尝试distutils无法识别我的MS
编译器的安装。
我在想,有没有更简单的方法在python中使用5或6位整数?即使是普通的8位也没关系。我只需要代表32或64个不同的数字。



你可以有效地将它们存储在数组中,例如,对于无符号字节

(或''b''代替''B''用于签名):

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



3


我们只能推测在进一步的帮助,直到你告诉我们你在做什么;-)


问候,

Bengt Richter

Glen Wheeler< ad ****** @ tpg.com.au>写道:

我的程序使用了数百万的整数,而Python为这些整数分配了太多的内存。使用磁盘我无法获得性能,所以我想我会写一个C扩展来定义一个新类型。
问题是,我的C知识已经存在多年了,而且无论如何我的
尝试distutils无法识别我的MS
编译器的安装。
我在想,有没有更简单的方法在python中使用5或6位整数?即使是普通的8位也没关系。我只需要代表32或64个不同的数字。




查看阵列模块的文档。

<格兰惠勒写道:

大家好,

我的程序使用了数百万的整数,而Python正在分配太多的内存这些。使用磁盘我无法获得性能,所以我想我会写一个C扩展来定义一个新类型。
问题是,我的C知识已经存在多年了,而且无论如何我的
尝试distutils无法识别我的MS
编译器的安装。
我在想,有没有更简单的方法在python中使用5或6位整数?即使是普通的8位也没关系。我只需要代表32或64个不同的数字。




你正试图解决错误的问题。 Python缓存小整数,所以

你只有非常少量的不同整数对象。这是一个很好的事情,因为每个Python对象都有8个字节的开销。

引入新类型不会节省任何内存。


真正的开销来自对这些对象的引用。每个

引用是4个字节(在32位计算机上),并且没有办法

减少这个。你最好的选择是在内部使用包含

许多整数的高级对象,但不要将它们存储为单独的Python

引用。阵列模块是一个选择,Numarray是另一个选择。如果

不变性不是问题,你甚至可以使用字符串(每个字符

视为8位整数)。

-

Rainer Deyke - ra*****@eldwood.com - http://eldwood.com



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.

thanks,
Glen

解决方案

On Fri, 19 Dec 2003 11:42:49 +1100, Glen Wheeler <ad******@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


Glen Wheeler <ad******@tpg.com.au> writes:

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.



Look at the docs for the array module.


Glen Wheeler 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''re trying to solve the wrong problem. Python caches small integers, so
you''ve only got a very small number of distinct integer objects. This is a
good thing, because each Python object has a eight byte overhead.
Introducing a new type wouldn''t result in any memory savings.

The real overhead comes from the references to those objects. Each
reference is four bytes (on 32 bit computers), and there is no way to
decrease this. Your best option is to use high level objects that contain
many integers internally but don''t store them as individual Python
references. The array module is one choice, Numarray is another. If
immutability isn''t an issue, you could even use strings (with each character
treated as an eight bit integer).
--
Rainer Deyke - ra*****@eldwood.com - http://eldwood.com


这篇关于如何在Python中使用5位或6位整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆