【发布时间】:2019-05-18 03:42:32
【问题描述】:
我需要将一个非常“高”的两列数组写入文本文件,而且速度非常慢。我发现如果我将数组重新整形为更宽的数组,写入速度会快得多。 例如
import time
import numpy as np
dataMat1 = np.random.rand(1000,1000)
dataMat2 = np.random.rand(2,500000)
dataMat3 = np.random.rand(500000,2)
start = time.perf_counter()
with open('test1.txt','w') as f:
np.savetxt(f,dataMat1,fmt='%g',delimiter=' ')
end = time.perf_counter()
print(end-start)
start = time.perf_counter()
with open('test2.txt','w') as f:
np.savetxt(f,dataMat2,fmt='%g',delimiter=' ')
end = time.perf_counter()
print(end-start)
start = time.perf_counter()
with open('test3.txt','w') as f:
np.savetxt(f,dataMat3,fmt='%g',delimiter=' ')
end = time.perf_counter()
print(end-start)
三个数据矩阵中的元素数量相同,为什么最后一个比其他两个更耗时?有什么办法可以加快“高”数据数组的写入速度?
【问题讨论】:
-
你真的需要它作为文本吗?
-
您可能需要检查this post 以获得高效的阵列 I/O
-
不幸的是,我需要将其写为文本而不是二进制文件。
标签: python performance numpy