Octave代码运行缓慢需优化:循环处理文件提速方案咨询
Hey there! I see your current loop-based approach for processing that text file is running too slow—let's fix that. The main issue with your code is that manual looping and repeated string splitting adds a lot of overhead, especially with larger files. Octave has built-in functions optimized for exactly this kind of numerical text data, so we can replace that entire block with something way more efficient.
Why Your Original Code Is Slow
Your current workflow does three steps that add unnecessary overhead:
- Reads the entire file into a single string, then splits it into lines
- Loops through each line to split into individual string cells
- Uses
cellfunto convert each cell to a number
Each of these steps involves iterating over elements in Octave, which is much slower than using vectorized/optimized built-in functions.
Faster Alternatives
1. Use dlmread (Simplest & Fastest)
Since your data is space-separated numerical values, dlmread is perfect—it directly reads the file into a numerical matrix in one line:
f = dlmread("C:/Backup/Thermo Backups/2018-05-17.txt");
This function skips all the manual string splitting and conversion steps entirely, as it's designed specifically for delimited numerical data.
2. Use textscan (More Flexible)
If you ever need to handle more complex formatting (like mixed data types or specific column formats), textscan is a great option. It's still way faster than your loop:
# Open the file for reading fid = fopen("C:/Backup/Thermo Backups/2018-05-17.txt", "r"); # Read all columns as floats, collect into a single matrix data = textscan(fid, "%f %f %f %f %f %f %f %f %f", "CollectOutput", true); # Close the file handle fclose(fid); # Extract the numerical matrix from the cell output f = data{1};
The %f specifiers match your 9 columns of floating-point numbers, and CollectOutput combines them into one matrix instead of separate cells.
Quick Test
For your sample data, both methods will produce the same numerical matrix as your original code, but in a fraction of the time—especially as your file size grows.
内容的提问来源于stack exchange,提问作者tomkut

