Speeding up writing a very large text file
Mostra commenti meno recenti
I have to write very large text files (1-4 million lines; 100-400 MB) to disk. Writing binary output is NOT an option. To solve this problem I broke the output into blocks of 10000 lines and then used sprintf to write the formatted lines (each ending with a '\n') to a block of 10000 strings (dataop=string(10000,1)).
I have already opened the output file with the 'W' option and then successively write each block of strings using the command: fprintf(fid,dataop(:));
It is still taking an inordinate amount of time - each 1 million lines takes 60 minutes!
The machine is high-end: running Windows 10, 128GB RAM, dual Xeon 10 core processors, and WD Black 6TB drive. There are 5 other hard drives with are not active during the write process. I'm running 2017b
So - why is it so slow? Am I doing something stupid? (I'm more used to C++ than MATLAB)
2 Commenti
Stephen23
il 17 Set 2018
@davidwriter: are the data numeric, strings, or char vectors? Do you really need to call sprintf as an intermediate step, why not just call fprintf directly?
davidwriter
il 17 Set 2018
Risposta accettata
Più risposte (1)
KSSV
il 17 Set 2018
How about this approach?
S = rand(10000,3) ;
S = cellstr(num2str(S)) ;
fid = fopen('data.txt','w') ;
fprintf(fid,'%s\n',S{:});
fclose(fid) ;
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!