我比较了将numpy array
写入原始二进制文件的两种简单方法:
# method 1
import numpy
A = numpy.random.randint(1000, size=512*1024*1024) # 2 GB
with open('blah.bin', 'wb') as f:
f.write(A)
以及
# method 2
import numpy
A = numpy.random.randint(1000, size=512*1024*1024) # 2 GB
raw_input()
B = A.tostring() # check memory usage of the current process here : 4 GB are used !!
raw_input()
with open('blah.bin', 'wb') as f:
f.write(B)
在第二种方法中,内存使用量是原来的两倍(这里是4GB)!你知道吗
为什么.tostring()
经常用于将numpy数组写入文件?
(在http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tofile.html中,说明了numpy.ndarray.tofile()
可以等价于file.write(a.tostring())
)
将这样的数组写入磁盘时,方法1和方法2一样正确吗?
文档没有说
.tofile()
等价于file.write(a.tostring())
,它只提到后者来解释参数sep
在其值为""
时的行为。你知道吗在第二种方法中,创建数组
A
的副本,存储在B
,然后写入文件,而在第一种方法中,避免使用中间副本。你知道吗你还应该看看:
相关问题 更多 >
编程相关推荐