0
0
Embedded Cprogramming~5 mins

Sensor reading through ADC (temperature, light) in Embedded C - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Sensor reading through ADC (temperature, light)
O(n)
Understanding Time 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.

Scenario Under Consideration

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

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The for-loop that calls readADC() repeatedly.
  • How many times: Exactly count times, once per sensor reading.
How Execution Grows With Input

Each additional sensor reading adds one more call to readADC(), so work grows steadily.

Input Size (count)Approx. Operations
1010 calls to readADC()
100100 calls to readADC()
10001000 calls to readADC()

Pattern observation: The total work grows directly with the number of readings.

Final Time Complexity

Time Complexity: O(n)

This means if you double the number of sensor readings, the time to complete also roughly doubles.

Common Mistake

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

Interview Connect

Understanding how sensor reading time grows helps you design efficient embedded programs and explain your code clearly in interviews.

Self-Check

"What if we read sensors in parallel instead of one by one? How would the time complexity change?"