记忆问题 [英] Memory problem
问题描述
我需要将大量数据读入列表。所以我想试试
看看我是否有任何内存问题。当我这样做时
x =范围(2700 * 2700 * 3)我收到以下消息:
Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
MemoryError
有什么方法可以解决这个问题吗?我有一台4G内存的机器。
我需要阅读的数据点(浮点数)总数为$ 200
$ b。
谢谢。
易兴写道:
I需要将大量数据读入列表。所以我想试试
看看我是否有任何内存问题。当我这样做时
x =范围(2700 * 2700 * 3)我收到以下消息:
Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
MemoryError
有什么方法可以解决这个问题?我有一台4G内存的机器。
我需要读取的数据点(浮点数)总数为200-300百万美元的b / b
。
如果你知道你只需要浮点数,那么你可以使用一个类型数组
(一个array.array)而不是一个无类型数组数组(一个Python列表):
导入数组
a = array.array(" f")
你也可以尝试像scipy这样的数值库,它可以支持
到2 GB的长阵列。
再见,
bearophile
Yi Xing写道:
我需要将大量数据读入列表。所以我想试试
看看我是否有任何内存问题。当我这样做时
x =范围(2700 * 2700 * 3)我收到以下消息:
Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
MemoryError
有什么方法可以解决这个问题吗?我有一台4G内存的机器。
我需要读取的数据点(浮点数)的总数是订单的价值
为200-300百万。
2700 * 2700 * 3仅为21M。你的电脑不应该流汗,
更不用说MemoryError了。 10次让我在1GB
机器上遇到MemoryError。
原始Python浮点数占用8个字节。在32位机器上,浮动对象
将有另外8个字节(type,refcount)。而不是列表,你可能需要使用array.array(它适用于同质的
内容,因此每个浮点数需要8个字节,而不是16个),或者也许
numeric / numpy / scipy / ...
HTH,
John
是********* ***@lycos.com 写道:
如果你知道你只需要花车,那么你可以使用一个打字阵列
(一个array.array)而不是一个无类型数组(一个Python列表):
导入数组
a = array.array(" f") ;)
澄清:typecode''f''存储一个Python浮点数(64位,相当于
到C的两倍)一个32位的FP编号(相当于一个C浮点数) - 除了明显的精度损失之外还有
,转换为&需要一点额外的时间
;来回。您可以考虑权衡
值得。
干杯,
John
Hi,
I need to read a large amount of data into a list. So I am trying to
see if I''ll have any memory problem. When I do
x=range(2700*2700*3) I got the following message:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
MemoryError
Any way to get around this problem? I have a machine of 4G memory. The
total number of data points (float) that I need to read is in the order
of 200-300 millions.
Thanks.
Yi Xing wrote:I need to read a large amount of data into a list. So I am trying to
see if I''ll have any memory problem. When I do
x=range(2700*2700*3) I got the following message:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
MemoryError
Any way to get around this problem? I have a machine of 4G memory. The
total number of data points (float) that I need to read is in the order
of 200-300 millions.If you know that you need floats only, then you can use a typed array
(an array.array) instead of an untyped array (a Python list):
import array
a = array.array("f")
You can also try with a numerical library like scipy, it may support up
to 2 GB long arrays.
Bye,
bearophile
Yi Xing wrote:Hi,
I need to read a large amount of data into a list. So I am trying to
see if I''ll have any memory problem. When I do
x=range(2700*2700*3) I got the following message:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
MemoryError
Any way to get around this problem? I have a machine of 4G memory. The
total number of data points (float) that I need to read is in the order
of 200-300 millions.
2700*2700*3 is only 21M. Your computer shouldn''t have raised a sweat,
let alone MemoryError. Ten times that got me a MemoryError on a 1GB
machine.
A raw Python float takes up 8 bytes. On a 32-bit machine a float object
will have another 8 bytes of (type, refcount). Instead of a list, you
probably need to use an array.array (which works on homogenous
contents, so it costs 8 bytes each float, not 16), or perhaps
numeric/numpy/scipy/...
HTH,
John
be************@lycos.com wrote:
If you know that you need floats only, then you can use a typed array
(an array.array) instead of an untyped array (a Python list):
import array
a = array.array("f")
Clarification: typecode ''f'' stores a Python float (64-bits, equivalent
to a C double) as a 32-bit FP number (equivalent to a C float) -- with
apart from the obvious loss of precision, a little extra time being
required to convert to & fro. You may consider the trade-off
worthwhile.
Cheers,
John
这篇关于记忆问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!