0
0
Embedded Cprogramming~5 mins

Multi-channel ADC scanning in Embedded C - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Multi-channel ADC scanning
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The for-loop that calls readADC for each channel.
  • How many times: Exactly once per channel, so numChannels times.
How Execution Grows With Input

Each additional channel adds one more ADC read operation, so the total work grows steadily.

Input Size (n)Approx. Operations
1010 ADC reads
100100 ADC reads
10001000 ADC reads

Pattern observation: The time grows directly in proportion to the number of channels scanned.

Final Time Complexity

Time Complexity: O(n)

This means the scanning time increases linearly as you add more ADC channels.

Common Mistake

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

Interview Connect

Understanding how scanning multiple sensors affects time helps you design efficient embedded systems and shows you can think about performance in real hardware tasks.

Self-Check

"What if we used DMA to read all ADC channels in parallel? How would the time complexity change?"