作者sppmg (sppmg)
看板MATLAB
标题[讨论] 快速读ascii的大资料档
时间Tue May 27 22:55:14 2014
这里要读的档案属於中等体积。
也就是很大,但记忆体还够整个载入那种。
我有些资料档为纯文字,格式如下
0 0.00062 -0.00719
0.00025 -0.00938 0.01125
0.0005 -0.03875 0.03156
由於这个txt档大小有260MB,包含 10015744 * 3 的矩阵。
所以我用load会非常慢。
推测是因为矩阵大小为动态增加。matlab 会不断消耗时间於要求记忆体上。
下面是我用桌电经由区网取得笔电上的数据。
(简单说就是读档速度较慢,运算较快)
在笔电上load mat 档一样是3s,但load txt 则是61s
% load mat
>> tic;data1=load('filename.mat');toc
Elapsed time is 3.642629 seconds.
% load text
>> tic;data2=load('filename.txt');toc
Elapsed time is 23.262854 seconds.
% load text by other method.
>> tic;tmp_s=fread(fid,inf,'uint8=>char');toc; ...
data_col_num=numel(sscanf(strtok(tmp_s,char(10)),'%f'));toc; ...
data3=sscanf(tmp_s,'%f',[data_col_num,inf])';toc; ...
clearvars tmp_s;toc
Elapsed time is 1.350866 seconds.
Elapsed time is 1.372573 seconds.
Elapsed time is 6.864294 seconds.
Elapsed time is 6.946270 seconds.
--------------------------------------
我最终的方法约为4倍速度
各位有没有更好的方法呢?
另外有点疑惑。
最後的sscanf中,如果用'%f %f %f' 时间会增加约1倍。
为何???
附上stackoverflow上其他人的尝试
http://stackoverflow.com/questions/9440592/fastest-matlab-file-reading
or
http://goo.gl/vmOetY
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.134.200.117
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/MATLAB/M.1401202518.A.43A.html
※ 编辑: sppmg (220.134.200.117), 05/27/2014 22:56:03
※ 编辑: sppmg (220.134.200.117), 05/27/2014 22:59:37
1F:推 sunev:这麽单纯又这麽大的矩阵就不要存成纯文字格式吧 05/27 22:58
2F:→ sppmg:因为我的来源就是txt啊!而且之後要用gnuplot 05/27 23:00
3F:推 dioann:试试textscan 我自己模拟一笔资料 load 51秒 textscan 11秒 07/14 17:14