0
0
Computer Networksknowledge~5 mins

TCP vs UDP comparison in Computer Networks - Performance Comparison

Choose your learning style9 modes available
Time Complexity: TCP vs UDP comparison
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations
  • 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.
How Execution Grows With Input

As data size grows, the number of packets grows roughly in proportion.

Input Size (n)Approx. Operations for TCPApprox. Operations for UDP
10 packets10 sends + 10 waits10 sends
100 packets100 sends + 100 waits100 sends
1000 packets1000 sends + 1000 waits1000 sends

Pattern observation: Both protocols scale linearly with data size, but TCP has extra waiting steps per packet.

Final Time Complexity

Time Complexity: O(n)

This means the time to send data grows directly in proportion to the number of packets sent.

Common Mistake

[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.

Interview Connect

Understanding how TCP and UDP scale helps you explain network performance clearly and confidently in real-world discussions.

Self-Check

What if TCP used batch acknowledgments instead of waiting after each packet? How would the time complexity change?