HTTP requests from Arduino - Time & Space Complexity
When an Arduino sends HTTP requests, it performs steps repeatedly to communicate with a server.
We want to understand how the time it takes grows as the number of requests increases.
Analyze the time complexity of the following code snippet.
#include <WiFi.h>
#include <HTTPClient.h>
void sendRequests(int n) {
for (int i = 0; i < n; i++) {
HTTPClient http;
http.begin("http://example.com/data");
int code = http.GET();
http.end();
}
}
This code sends n HTTP GET requests one after another to a server.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending an HTTP GET request inside the loop.
- How many times: Exactly
ntimes, once per loop iteration.
Each request takes roughly the same time, so total time grows as we send more requests.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 HTTP requests |
| 100 | 100 HTTP requests |
| 1000 | 1000 HTTP requests |
Pattern observation: The total time grows directly in proportion to the number of requests.
Time Complexity: O(n)
This means if you double the number of requests, the total time roughly doubles too.
[X] Wrong: "Sending multiple HTTP requests happens all at once, so time stays the same no matter how many requests."
[OK] Correct: Each request waits for a response before the next starts, so time adds up with each request.
Understanding how repeated network calls affect time helps you design better programs and explain your choices clearly.
"What if we sent all HTTP requests at the same time using asynchronous calls? How would the time complexity change?"