简体   繁体   English

如何在Python中存储单字节内存?

[英]How to store single byte of memory in Python?

How can I allocate/store a single or couple of bytes (eg 2 or 4) bytes of information in Python ? 如何在Python中分配/存储单个或几个字节(例如2或4)字节的信息?

I am not looking for alternative of malloc/new in Python but may be some datatype which doesn't take huge amount of memory. 我不是在寻找Python中malloc / new的替代品,但可能是一些不占用大量内存的数据类型。

I tried the following but as shown below, all are taking huge amount of memory. 我尝试了以下但是如下所示,所有都占用了大量的内存。

Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> i = 1 ; sys.getsizeof(i)
24
>>> i = None ; sys.getsizeof(i)
16
>>> i = 'c' ; sys.getsizeof(i)
38
>>> i = 'good' ; sys.getsizeof(i)
41
>>> i = bytearray(0) ; sys.getsizeof(i)
48
>>> i = bytearray(1) ; sys.getsizeof(i)
50
>>> from struct import *
>>> i = pack('h', 1) ; sys.getsizeof(i)
39
>>> i = array('l', [1]) ; sys.getsizeof(i)
64L

I love Python and am writing an application which will be storing some 100,000 firewall rules. 我喜欢Python并且正在编写一个应用程序,它将存储大约100,000个防火墙规则。 Each rule will be some 500 bytes of information if I use conventional datatypes (integer, string) of Python. 如果我使用Python的常规数据类型(整数,字符串),每条规则将是大约500字节的信息。 I want to save the space and avoid switching to C/C++ too as most of the rest of the application is in Python (2.7). 我想节省空间并避免切换到C / C ++,因为大多数应用程序的其余部分都在Python(2.7)中。

Also, I can not persist the memory as my application will check for update or modification of rules almost every 2 minutes. 此外,我无法保留内存,因为我的应用程序几乎每2分钟检查一次规则的更新或修改。

My idea is to save memory by compressing the information. 我的想法是通过压缩信息来节省内存。 For example, instead of storing the 'direction' of a rule as 'input' or 'output' or 'inout' in a string or integer, I would dedicate 2 or 3 bits for marking the particular direction. 例如,不是将规则的“方向”存储为字符串或整数中的“输入”或“输出”或“输出”,而是将2或3位用于标记特定方向。 With that I am assuming my one rule information can be saved into less than 10 bytes. 有了这个我假设我的一个规则信息可以保存到少于10个字节。 For this, I want to know a method of storing only 2/4 bytes of information. 为此,我想知道一种只存储2/4字节信息的方法。

Appreciate your feedback / suggestions / pointers. 感谢您的反馈/建议/指针。

In measuring your sizes you didn't take care to exclude underlying class overhead from the size of the data stored. 在测量您的大小时,您没有注意从存储的数据大小中排除基础类开销。 For example, below shows bytearray has about 48 bytes of overhead, but then each byte added takes about 1 byte. 例如,下面显示了bytearray有大约48个字节的开销,但是每个添加的字节大约需要1个字节。 I presume the jump from 50 bytes to 53 to 56 indicates memory access optimization. 我假设从50字节到53到56的跳转表示存储器访问优化。

>>> i = bytearray()
>>> sys.getsizeof(i)
48
>>> i = bytearray((1))
>>> sys.getsizeof(i)
50
>>> i = bytearray((1,2))
>>> sys.getsizeof(i)
53
>>> i = bytearray((1,2,3))
>>> sys.getsizeof(i)
53
>>> i = bytearray((1,2,3,4))
>>> sys.getsizeof(i)
53
>>> i = bytearray((1,2,3,4,5))
>>> sys.getsizeof(i)
56

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM