Python论坛  - 讨论区

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

2004年08月10日 星期二 08:05

tocer tootoo at yeah.net
Tue Aug 10 08:05:54 HKT 2004

比如:
a=[1,2,3]
测试 a 在内存中的所占的字节数

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

2004年08月10日 星期二 11:05

gentoo.cn gentoo.cn at 126.com
Tue Aug 10 11:05:42 HKT 2004

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
>  
>



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

2004年08月10日 星期二 11:57

Jacob Fan jacob at exoweb.net
Tue Aug 10 11:57:02 HKT 2004

这个不行,只能用来计算struct模块生成的c struct的大小。
计算Python对象占用空间的模块不知道有没有人写了。如果没人写的话,应该是一个新的项目的好题材 :) 
----- Original Message ----- 
From: "gentoo.cn" <gentoo.cn at 126.com>
To: <python-chinese at lists.python.cn>
Sent: Tuesday, August 10, 2004 11:05 AM
Subject: Re: [python-chinese] 如何测量一个变量或者常量在内存中的大小


> 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
> 
> 

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

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

Xie Yanbo idkey at 163.com
Tue Aug 10 12:08:44 HKT 2004

On 2004-08-10 11:57:1092110222 +0800, Jacob Fan wrote:
> 这个不行,只能用来计算struct模块生成的c struct的大小。
> 计算Python对象占用空间的模块不知道有没有人写了。如果没人写的话,应该是一个新的项目的好题材 :) 

记得 python 中有一个查看占用内存的函数,在建立新对象前后分别运行一下,
就可以看到新对象占用了多少空间。不过我忘记这是那个函数了。



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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号