CSV library basics in Ruby - Time & Space Complexity
When working with CSV files in Ruby, it's important to understand how the time to process data grows as the file gets bigger.
We want to know how the program's work changes when the number of rows in the CSV increases.
Analyze the time complexity of the following code snippet.
require 'csv'
CSV.foreach('data.csv') do |row|
puts row[0]
end
This code reads a CSV file line by line and prints the first value of each row.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each row from the CSV file one by one.
- How many times: Once for every row in the CSV file.
As the number of rows grows, the program reads and processes more lines.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads and prints |
| 100 | 100 reads and prints |
| 1000 | 1000 reads and prints |
Pattern observation: The work grows directly with the number of rows; doubling rows doubles work.
Time Complexity: O(n)
This means the time to read and process the CSV grows in a straight line with the number of rows.
[X] Wrong: "Reading a CSV file is always instant no matter the size."
[OK] Correct: The program must read each row one by one, so bigger files take more time.
Understanding how file reading scales helps you write efficient data processing code and answer questions about handling large files.
"What if we used CSV.read to load all rows at once instead of CSV.foreach? How would the time complexity change?"