Python论坛  - 讨论区

标题:[python-chinese] 如何测量一个变量或者常量在内存中的大小

2004年08月10日 星期二 12:43

gentoo.cn gentoo.cn at 126.com
Tue Aug 10 12:43:24 HKT 2004

我的意思是:是不是可以通过计算一个Object的Struct来猜测这个Object的大小?
比如c='a', string="abcd"
那么c的类型是1c, string的类型是4c
calcsize('c')=1
calcsize('L')=4
也就是说一个数值型的对象占用4Bytes内存空间,一个字符型对象占用1Bytes内存
空间?
对于string="abcd"这个对象,类型是4c所以占用4Bytes内存空间?
不知道Python如何考虑对齐的(也许他最小的单位是1Bytes所以都是8bit的倍数,
这样就对齐了?)

大家都来讨论讨论啊。



limodou wrote:

>gentoo.cn,您好!
>
>	那个不是内存的大小吧。而是对应的struct的大小。因为在python中都是对象,而不仅仅是数据结构。不知道:你想知道内存大小有什么用?
>
>======= 2004-08-10 11:05:42 您在来信中写道:=======
>
>  
>
>>import struct
>>struct.pack
>>struct.calcsize ?
>>
>>难道使用这个方法吗?
>>我也想知道有什么方法来计算对象的size
>>
>>*calcsize*( 	fmt)
>>
>>   Return the size of the struct (and hence of the string)
>>   corresponding to the given format. 
>>
>>Format characters have the following meaning; the conversion between C
>>and Python values should be obvious given their types:
>>
>>*Format* 	*C Type* 	*Python* 	*Notes*
>>x 	pad byte 	no value 	
>>c 	char 	string of length 1 	
>>b 	signed char 	integer 	
>>B 	unsigned char 	integer 	
>>h 	short 	integer 	
>>H 	unsigned short 	integer 	
>>i 	int 	integer 	
>>I 	unsigned int 	long 	
>>l 	long 	integer 	
>>L 	unsigned long 	long 	
>>q 	long long 	long 	(1)
>>Q 	unsigned long long 	long 	(1)
>>f 	float 	float 	
>>d 	double 	float 	
>>s 	char[] 	string 	
>>p 	char[] 	string 	
>>P 	void * 	integer 	
>>
>>
>>tocer wrote:
>>
>>    
>>
>>>比如:
>>>a=[1,2,3]
>>>测试 a 在内存中的所占的字节数
>>>
>>>------------------------------------------------------------------------
>>>
>>>_______________________________________________
>>>python-chinese list
>>>python-chinese at lists.python.cn
>>>http://python.cn/mailman/listinfo/python-chinese
>>> 
>>>
>>>      
>>>
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>>
>>    
>>
>
>= = = = = = = = = = = = = = = = = = = =
>			
>
>        致
>礼!
> 
>				 
>        limodou
>        chatme at 263.net
>          2004-08-10
>
>  
>
>------------------------------------------------------------------------
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>  
>



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月10日 星期二 12:53

gentoo.cn gentoo.cn at 126.com
Tue Aug 10 12:53:49 HKT 2004

找到一个相关的讨论
http://www.egenix.com/mailman-archives/egenix-users/2002-November/000156.html

gentoo.cn wrote:

>我的意思是:是不是可以通过计算一个Object的Struct来猜测这个Object的大小?
>比如c='a', string="abcd"
>那么c的类型是1c, string的类型是4c
>calcsize('c')=1
>calcsize('L')=4
>也就是说一个数值型的对象占用4Bytes内存空间,一个字符型对象占用1Bytes内存
>空间?
>对于string="abcd"这个对象,类型是4c所以占用4Bytes内存空间?
>不知道Python如何考虑对齐的(也许他最小的单位是1Bytes所以都是8bit的倍数,
>这样就对齐了?)
>
>大家都来讨论讨论啊。
>
>
>
>limodou wrote:
>
>  
>
>>gentoo.cn,您好!
>>
>>	那个不是内存的大小吧。而是对应的struct的大小。因为在python中都是对象,而不仅仅是数据结构。不知道:你想知道内存大小有什么用?
>>
>>======= 2004-08-10 11:05:42 您在来信中写道:=======
>>
>> 
>>
>>    
>>
>>>import struct
>>>struct.pack
>>>struct.calcsize ?
>>>
>>>难道使用这个方法吗?
>>>我也想知道有什么方法来计算对象的size
>>>
>>>*calcsize*( 	fmt)
>>>
>>>  Return the size of the struct (and hence of the string)
>>>  corresponding to the given format. 
>>>
>>>Format characters have the following meaning; the conversion between C
>>>and Python values should be obvious given their types:
>>>
>>>*Format* 	*C Type* 	*Python* 	*Notes*
>>>x 	pad byte 	no value 	
>>>c 	char 	string of length 1 	
>>>b 	signed char 	integer 	
>>>B 	unsigned char 	integer 	
>>>h 	short 	integer 	
>>>H 	unsigned short 	integer 	
>>>i 	int 	integer 	
>>>I 	unsigned int 	long 	
>>>l 	long 	integer 	
>>>L 	unsigned long 	long 	
>>>q 	long long 	long 	(1)
>>>Q 	unsigned long long 	long 	(1)
>>>f 	float 	float 	
>>>d 	double 	float 	
>>>s 	char[] 	string 	
>>>p 	char[] 	string 	
>>>P 	void * 	integer 	
>>>
>>>
>>>tocer wrote:
>>>
>>>   
>>>
>>>      
>>>
>>>>比如:
>>>>a=[1,2,3]
>>>>测试 a 在内存中的所占的字节数
>>>>
>>>>------------------------------------------------------------------------
>>>>
>>>>_______________________________________________
>>>>python-chinese list
>>>>python-chinese at lists.python.cn
>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>
>>>>
>>>>     
>>>>
>>>>        
>>>>
>>>_______________________________________________
>>>python-chinese list
>>>python-chinese at lists.python.cn
>>>http://python.cn/mailman/listinfo/python-chinese
>>>
>>>   
>>>
>>>      
>>>
>>= = = = = = = = = = = = = = = = = = = =
>>			
>>
>>        致
>>礼!
>>
>>				 
>>        limodou
>>        chatme at 263.net
>>          2004-08-10
>>
>> 
>>
>>------------------------------------------------------------------------
>>
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>> 
>>
>>    
>>
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>
>
>  
>



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月10日 星期二 14:23

Jacob Fan jacob at exoweb.net
Tue Aug 10 14:23:48 HKT 2004

Python的对象不是按照C的方式摆放的,你的方法不对。首先,每个对象(包括数字,字符串等)都有一个同样的头部,后面的部分每个对象不同。list的实际实现是一个c的array,一般的instance用字典来存放属性值。具体实现你要去看Python的源码,在这儿凭空猜测没有用。
----- Original Message ----- 
From: "gentoo.cn" <gentoo.cn at 126.com>
To: <python-chinese at lists.python.cn>
Sent: Tuesday, August 10, 2004 12:43 PM
Subject: Re: [python-chinese] 如何测量一个变量或者常量在内存中的大小


> 我的意思是:是不是可以通过计算一个Object的Struct来猜测这个Object的大小?
> 比如c='a', string="abcd"
> 那么c的类型是1c, string的类型是4c
> calcsize('c')=1
> calcsize('L')=4
> 也就是说一个数值型的对象占用4Bytes内存空间,一个字符型对象占用1Bytes内存
> 空间?
> 对于string="abcd"这个对象,类型是4c所以占用4Bytes内存空间?
> 不知道Python如何考虑对齐的(也许他最小的单位是1Bytes所以都是8bit的倍数,
> 这样就对齐了?)
> 
> 大家都来讨论讨论啊。
> 
> 
> 
> limodou wrote:
> 
> >gentoo.cn,您好!
> >
> > 那个不是内存的大小吧。而是对应的struct的大小。因为在python中都是对象,而不仅仅是数据结构。不知道:你想知道内存大小有什么用?
> >
> >======= 2004-08-10 11:05:42 您在来信中写道:=======
> >
> >  
> >
> >>import struct
> >>struct.pack
> >>struct.calcsize ?
> >>
> >>难道使用这个方法吗?
> >>我也想知道有什么方法来计算对象的size
> >>
> >>*calcsize*( fmt)
> >>
> >>   Return the size of the struct (and hence of the string)
> >>   corresponding to the given format. 
> >>
> >>Format characters have the following meaning; the conversion between C
> >>and Python values should be obvious given their types:
> >>
> >>*Format* *C Type* *Python* *Notes*
> >>x pad byte no value 
> >>c char string of length 1 
> >>b signed char integer 
> >>B unsigned char integer 
> >>h short integer 
> >>H unsigned short integer 
> >>i int integer 
> >>I unsigned int long 
> >>l long integer 
> >>L unsigned long long 
> >>q long long long (1)
> >>Q unsigned long long long (1)
> >>f float float 
> >>d double float 
> >>s char[] string 
> >>p char[] string 
> >>P void * integer 
> >>
> >>
> >>tocer wrote:
> >>
> >>    
> >>
> >>>比如:
> >>>a=[1,2,3]
> >>>测试 a 在内存中的所占的字节数
> >>>
> >>>------------------------------------------------------------------------
> >>>
> >>>_______________________________________________
> >>>python-chinese list
> >>>python-chinese at lists.python.cn
> >>>http://python.cn/mailman/listinfo/python-chinese
> >>> 
> >>>
> >>>      
> >>>
> >>_______________________________________________
> >>python-chinese list
> >>python-chinese at lists.python.cn
> >>http://python.cn/mailman/listinfo/python-chinese
> >>
> >>    
> >>
> >
> >= = = = = = = = = = = = = = = = = = = =
> > 
> >
> >        致
> >礼!
> > 
> > 
> >        limodou
> >        chatme at 263.net
> >          2004-08-10
> >
> >  
> >
> >------------------------------------------------------------------------
> >
> >_______________________________________________
> >python-chinese list
> >python-chinese at lists.python.cn
> >http://python.cn/mailman/listinfo/python-chinese
> >  
> >
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 
> 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月10日 星期二 15:33

tocer tootoo at yeah.net
Tue Aug 10 15:33:00 HKT 2004

没想到这么难解决

----- Original Message ----- 
From: "gentoo.cn" <gentoo.cn at 126.com>
To: <python-chinese at lists.python.cn>
Sent: Tuesday, August 10, 2004 12:53 PM
Subject: Re: [python-chinese] 如何测量一个变量或者常量在内存中的大小


> 找到一个相关的讨论
> http://www.egenix.com/mailman-archives/egenix-users/2002-November/000156.html
> 
> gentoo.cn wrote:
> 
> >我的意思是:是不是可以通过计算一个Object的Struct来猜测这个Object的大小?
> >比如c='a', string="abcd"
> >那么c的类型是1c, string的类型是4c
> >calcsize('c')=1
> >calcsize('L')=4
> >也就是说一个数值型的对象占用4Bytes内存空间,一个字符型对象占用1Bytes内存
> >空间?
> >对于string="abcd"这个对象,类型是4c所以占用4Bytes内存空间?
> >不知道Python如何考虑对齐的(也许他最小的单位是1Bytes所以都是8bit的倍数,
> >这样就对齐了?)
> >
> >大家都来讨论讨论啊。
> >
> >
> >
> >limodou wrote:
> >
> >  
> >
> >>gentoo.cn,您好!
> >>
> >> 那个不是内存的大小吧。而是对应的struct的大小。因为在python中都是对象,而不仅仅是数据结构。不知道:你想知道内存大小有什么用?
> >>
> >>======= 2004-08-10 11:05:42 您在来信中写道:=======
> >>
> >> 
> >>
> >>    
> >>
> >>>import struct
> >>>struct.pack
> >>>struct.calcsize ?
> >>>
> >>>难道使用这个方法吗?
> >>>我也想知道有什么方法来计算对象的size
> >>>
> >>>*calcsize*( fmt)
> >>>
> >>>  Return the size of the struct (and hence of the string)
> >>>  corresponding to the given format. 
> >>>
> >>>Format characters have the following meaning; the conversion between C
> >>>and Python values should be obvious given their types:
> >>>
> >>>*Format* *C Type* *Python* *Notes*
> >>>x pad byte no value 
> >>>c char string of length 1 
> >>>b signed char integer 
> >>>B unsigned char integer 
> >>>h short integer 
> >>>H unsigned short integer 
> >>>i int integer 
> >>>I unsigned int long 
> >>>l long integer 
> >>>L unsigned long long 
> >>>q long long long (1)
> >>>Q unsigned long long long (1)
> >>>f float float 
> >>>d double float 
> >>>s char[] string 
> >>>p char[] string 
> >>>P void * integer 
> >>>
> >>>
> >>>tocer wrote:
> >>>
> >>>   
> >>>
> >>>      
> >>>
> >>>>比如:
> >>>>a=[1,2,3]
> >>>>测试 a 在内存中的所占的字节数
> >>>>
> >>>>------------------------------------------------------------------------
> >>>>
> >>>>_______________________________________________
> >>>>python-chinese list
> >>>>python-chinese at lists.python.cn
> >>>>http://python.cn/mailman/listinfo/python-chinese
> >>>>
> >>>>
> >>>>     
> >>>>
> >>>>        
> >>>>
> >>>_______________________________________________
> >>>python-chinese list
> >>>python-chinese at lists.python.cn
> >>>http://python.cn/mailman/listinfo/python-chinese
> >>>
> >>>   
> >>>
> >>>      
> >>>
> >>= = = = = = = = = = = = = = = = = = = =
> >> 
> >>
> >>        致
> >>礼!
> >>
> >> 
> >>        limodou
> >>        chatme at 263.net
> >>          2004-08-10
> >>
> >> 
> >>
> >>------------------------------------------------------------------------
> >>
> >>_______________________________________________
> >>python-chinese list
> >>python-chinese at lists.python.cn
> >>http://python.cn/mailman/listinfo/python-chinese
> >> 
> >>
> >>    
> >>
> >
> >_______________________________________________
> >python-chinese list
> >python-chinese at lists.python.cn
> >http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >  
> >
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号