TCP vs UDP comparison in Computer Networks - Performance Comparison
When comparing TCP and UDP, it's important to understand how their operations scale as data size grows.
We want to see how the time to send data changes with the amount of data and protocol features.
Analyze the time complexity of sending data packets using TCP and UDP.
// Pseudocode for sending data
function sendData(protocol, dataSize) {
for (packet in splitData(dataSize)) {
sendPacket(packet)
if (protocol == 'TCP') {
waitForAck()
}
}
}
This code sends data split into packets. TCP waits for acknowledgment after each packet; UDP sends packets without waiting.
- Primary operation: Loop over packets to send data.
- How many times: Once per packet, proportional to data size.
- Additional operation for TCP: Waiting for acknowledgment after each packet.
As data size grows, the number of packets grows roughly in proportion.
| Input Size (n) | Approx. Operations for TCP | Approx. Operations for UDP |
|---|---|---|
| 10 packets | 10 sends + 10 waits | 10 sends |
| 100 packets | 100 sends + 100 waits | 100 sends |
| 1000 packets | 1000 sends + 1000 waits | 1000 sends |
Pattern observation: Both protocols scale linearly with data size, but TCP has extra waiting steps per packet.
Time Complexity: O(n)
This means the time to send data grows directly in proportion to the number of packets sent.
[X] Wrong: "UDP is always faster because it has no waiting steps, so it has constant time complexity."
[OK] Correct: UDP still sends each packet one by one, so time grows with data size, not constant.
Understanding how TCP and UDP scale helps you explain network performance clearly and confidently in real-world discussions.
What if TCP used batch acknowledgments instead of waiting after each packet? How would the time complexity change?