Reading text files (readtable, textscan) in MATLAB - Time & Space Complexity
When reading text files in MATLAB, it's important to know how the time to read grows as the file gets bigger.
We want to understand how the reading time changes when the file size increases.
Analyze the time complexity of the following code snippet.
filename = 'data.txt';
fileID = fopen(filename, 'r');
C = textscan(fileID, '%s %f %d', 'Delimiter', ',');
fclose(fileID);
T = readtable(filename, 'Delimiter', ',');
This code reads a text file twice: once using textscan and once using readtable, both reading all lines.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each line of the file and parsing its contents.
- How many times: Once for each line in the file, so the number of lines (n) times.
As the file grows, the program reads more lines, so the work grows roughly in direct proportion to the number of lines.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 line reads and parses |
| 100 | About 100 line reads and parses |
| 1000 | About 1000 line reads and parses |
Pattern observation: The time grows steadily as the file gets bigger, roughly doubling if the file size doubles.
Time Complexity: O(n)
This means the time to read the file grows linearly with the number of lines in the file.
[X] Wrong: "Reading a file takes the same time no matter how big it is."
[OK] Correct: The program reads each line one by one, so more lines mean more work and more time.
Understanding how file reading time grows helps you write efficient programs and explain your code clearly in interviews.
"What if we read only the first 100 lines instead of the whole file? How would the time complexity change?"