FTP and SFTP for file transfer in Computer Networks - Time & Space Complexity
When transferring files using FTP or SFTP, it's important to understand how the time taken grows as the file size increases.
We want to know how the transfer time changes when files get bigger or when many files are sent.
Analyze the time complexity of this simplified file transfer process.
function transferFiles(files) {
for (let file of files) {
openConnection();
sendFile(file); // sends file data byte by byte
closeConnection();
}
}
This code sends each file one after another, opening and closing the connection each time, sending data byte by byte.
Look at what repeats in this process:
- Primary operation: Sending each byte of every file.
- How many times: For each file, the number of bytes in that file.
The main time cost comes from sending all bytes one by one.
As file sizes grow, the time to send them grows too.
| Input Size (total bytes) | Approx. Operations (bytes sent) |
|---|---|
| 10 KB | 10,240 |
| 100 KB | 102,400 |
| 1 MB | 1,048,576 |
Pattern observation: The time grows roughly in direct proportion to the total number of bytes sent.
Time Complexity: O(n)
This means the time to transfer files grows linearly with the total size of the files.
[X] Wrong: "Opening and closing the connection each time doesn't affect the total time much."
[OK] Correct: Opening and closing connections add extra time for each file, so doing it repeatedly can slow down the overall transfer significantly.
Understanding how file transfer time grows helps you explain network performance and design better transfer methods.
What if we kept the connection open for all files instead of opening and closing it each time? How would the time complexity change?