Sensor reading through ADC (temperature, light) in Embedded C - Time & Space Complexity
When reading sensors using ADC, it is important to know how the time to get data changes as we read more samples.
We want to see how the program's work grows when reading multiple sensor values.
Analyze the time complexity of the following code snippet.
// Read multiple sensor values using ADC
void readSensors(int *buffer, int count) {
for (int i = 0; i < count; i++) {
buffer[i] = readADC(); // Read one sensor value
}
}
int readADC() {
// Simulate ADC reading delay
return 42; // Dummy sensor value
}
This code reads sensor values one by one and stores them in a buffer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that calls
readADC()repeatedly. - How many times: Exactly
counttimes, once per sensor reading.
Each additional sensor reading adds one more call to readADC(), so work grows steadily.
| Input Size (count) | Approx. Operations |
|---|---|
| 10 | 10 calls to readADC() |
| 100 | 100 calls to readADC() |
| 1000 | 1000 calls to readADC() |
Pattern observation: The total work grows directly with the number of readings.
Time Complexity: O(n)
This means if you double the number of sensor readings, the time to complete also roughly doubles.
[X] Wrong: "Reading multiple sensors happens instantly and does not add time."
[OK] Correct: Each sensor reading takes time, so more readings mean more total time.
Understanding how sensor reading time grows helps you design efficient embedded programs and explain your code clearly in interviews.
"What if we read sensors in parallel instead of one by one? How would the time complexity change?"