什么是最快的方式写入矩阵到八度文本文件? [英] What is the fastest way to write a matrix to a text file in Octave?

查看:100
本文介绍了什么是最快的方式写入矩阵到八度文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大矩阵(2e6 x 3),我必须写入一个文本文件。

dlmwrite大约需要230秒才能完成这个任务。

从你的经验来说,写一个最快的方法是什么大矩阵到文本文件?

解决方案

以下适用于MATLAB,但是我建议你在Octave中试试。首先,如果你可以 - 转置矩阵。下面是使用 fprintf csvwrite (本质上是 dlmwrite )的例子。

  A = rand(3,1e6); 
tic;
fid = fopen('data.txt','w +');
for i = 1:size(A,1)
fprintf(fid,'%f',A(i,:));
fprintf(fid,'\\\
');
end
fclose(fid);
toc

tic;
csvwrite('data.txt',A);
toc;

已用时间为1.311512秒。
已用时间为2.487737秒。

如果不转置,确实需要很长时间。 默认情况下, fprintf 每次通话后刷新缓冲区。您可以尝试使用 W 来代替 w 来打开文件,但这并不能改善这里的情况。


I have a large matrix (2e6 x 3) which I have to write to a text file.

dlmwrite takes about 230s to achieve this task.

From your experience what is the fastest way to write a large matrix to a text file?

解决方案

The following applies to MATLAB, but I suggest you try it in Octave. First of all, if you can - transpose the matrix. Here are examples using fprintf and csvwrite (essentially dlmwrite)

A = rand(3, 1e6);
tic;
fid = fopen('data.txt', 'w+');
for i=1:size(A, 1)
    fprintf(fid, '%f ', A(i,:));
    fprintf(fid, '\n');
end
fclose(fid);
toc

tic;
csvwrite('data.txt', A);
toc;

Elapsed time is 1.311512 seconds.
Elapsed time is 2.487737 seconds.

If not transposed, it will take ages indeed. By default, fprintf flushes the buffer after every call. You can try to use W instead of w to open the file, but it does not improve the situation here too much.

这篇关于什么是最快的方式写入矩阵到八度文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆