Multi-channel ADC scanning in Embedded C - Time & Space Complexity
When reading multiple sensors using an ADC, the time it takes depends on how many channels we scan.
We want to know how the scanning time grows as we add more channels.
Analyze the time complexity of the following code snippet.
void scanADC(int *channels, int numChannels, int *results) {
for (int i = 0; i < numChannels; i++) {
results[i] = readADC(channels[i]);
}
}
int readADC(int channel) {
// Simulate ADC read delay
// Returns analog value for the channel
return channel * 10; // placeholder
}
This code reads analog values from multiple ADC channels one by one and stores the results.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that calls
readADCfor each channel. - How many times: Exactly once per channel, so
numChannelstimes.
Each additional channel adds one more ADC read operation, so the total work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 ADC reads |
| 100 | 100 ADC reads |
| 1000 | 1000 ADC reads |
Pattern observation: The time grows directly in proportion to the number of channels scanned.
Time Complexity: O(n)
This means the scanning time increases linearly as you add more ADC channels.
[X] Wrong: "Reading multiple ADC channels happens all at once, so time stays the same no matter how many channels."
[OK] Correct: Each channel requires a separate read operation, so time adds up with each channel scanned.
Understanding how scanning multiple sensors affects time helps you design efficient embedded systems and shows you can think about performance in real hardware tasks.
"What if we used DMA to read all ADC channels in parallel? How would the time complexity change?"