As 霍利指出 https://stackoverflow.com/a/53820971/190597, savetxt
is 循环遍历各行X https://github.com/numpy/numpy/blob/master/numpy/lib/npyio.py#L1420并单独格式化每一行:
for row in X:
try:
v = format % tuple(row) + newline
except TypeError:
raise TypeError("Mismatch between array dtype ('%s') and "
"format specifier ('%s')"
% (str(X.dtype), format))
fh.write(v)
我认为这里主要的时间杀手是所有字符串插值调用。
如果我们将所有字符串插值打包到一个调用中,事情就会变得更快:
with open('/tmp/test4.txt','w') as f:
fmt = ' '.join(['%g']*dataMat3.shape[1])
fmt = '\n'.join([fmt]*dataMat3.shape[0])
data = fmt % tuple(dataMat3.ravel())
f.write(data)
import io
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('/tmp/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('/tmp/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('/tmp/test3.txt','w') as f:
np.savetxt(f,dataMat3,fmt='%g',delimiter=' ')
end = time.perf_counter()
print(end-start)
start = time.perf_counter()
with open('/tmp/test4.txt','w') as f:
fmt = ' '.join(['%g']*dataMat3.shape[1])
fmt = '\n'.join([fmt]*dataMat3.shape[0])
data = fmt % tuple(dataMat3.ravel())
f.write(data)
end = time.perf_counter()
print(end-start)
reports
0.1604848340011813
0.17416274400056864
0.6634929459996783
0.16207673999997496