gzip and gunzip in Linux CLI - Time & Space Complexity
When using gzip and gunzip, it's important to understand how the time to compress or decompress files changes as file size grows.
We want to know how the work done by these commands grows when the input file gets bigger.
Analyze the time complexity of the following commands.
gzip largefile.txt
# Compresses the file largefile.txt into largefile.txt.gz
gunzip largefile.txt.gz
# Decompresses the file back to largefile.txt
These commands compress and decompress a file using gzip and gunzip.
Both gzip and gunzip process the file data in chunks, reading and writing repeatedly.
- Primary operation: Reading and processing each byte of the file.
- How many times: Once for each byte in the file, sequentially.
As the file size grows, the time to compress or decompress grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 KB | 10,240 operations |
| 100 KB | 102,400 operations |
| 1 MB (1024 KB) | 1,048,576 operations |
Pattern observation: Doubling the file size roughly doubles the work done.
Time Complexity: O(n)
This means the time to compress or decompress grows linearly with the file size.
[X] Wrong: "gzip compresses files instantly no matter the size."
[OK] Correct: Compression takes time proportional to file size because every byte must be processed.
Understanding how compression tools scale with file size helps you reason about performance in real tasks, showing you can think about efficiency beyond just writing commands.
"What if gzip used multi-threading to compress parts of the file in parallel? How would the time complexity change?"