0
0
Arduinoprogramming~5 mins

Sending data over Bluetooth in Arduino - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Sending data over Bluetooth
O(n)
Understanding Time Complexity

When sending data over Bluetooth with Arduino, it's important to know how the time to send grows as the data size grows.

We want to understand how the program's running time changes when we send more data.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


void sendData(char* data, int length) {
  for (int i = 0; i < length; i++) {
    Serial.write(data[i]);
  }
}
    

This code sends each byte of data one by one over Bluetooth using Serial.write.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Sending each byte with Serial.write inside a for loop.
  • How many times: The loop runs once for every byte in the data (length times).
How Execution Grows With Input

As the amount of data grows, the number of send operations grows the same way.

Input Size (n)Approx. Operations
1010 send operations
100100 send operations
10001000 send operations

Pattern observation: The time to send data grows directly with the number of bytes.

Final Time Complexity

Time Complexity: O(n)

This means the time to send data grows in a straight line as the data size grows.

Common Mistake

[X] Wrong: "Sending data over Bluetooth happens instantly no matter the size."

[OK] Correct: Each byte takes time to send, so sending more bytes takes more time.

Interview Connect

Understanding how sending data scales helps you write efficient code for devices that communicate wirelessly.

Self-Check

"What if we send data in chunks instead of byte by byte? How would the time complexity change?"