0
0
Embedded Cprogramming~5 mins

Configuring pin as input or output in Embedded C - Performance & Efficiency

Choose your learning style9 modes available
Time Complexity: Configuring pin as input or output
O(n)
Understanding Time Complexity

When configuring a pin as input or output, the code runs a few simple steps.

We want to see how the time it takes changes if we do this for many pins.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


void configure_pins(int pins[], int n) {
    for (int i = 0; i < n; i++) {
        if (pins[i] % 2 == 0) {
            // Set pin as output
            set_pin_output(pins[i]);
        } else {
            // Set pin as input
            set_pin_input(pins[i]);
        }
    }
}

This code sets each pin in the array as input or output based on whether the pin number is even or odd.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Looping through each pin in the array.
  • How many times: Exactly once for each pin, so n times.
How Execution Grows With Input

As the number of pins increases, the time to configure them grows in a straight line.

Input Size (n)Approx. Operations
1010 times setting pins
100100 times setting pins
10001000 times setting pins

Pattern observation: Doubling the pins doubles the work needed.

Final Time Complexity

Time Complexity: O(n)

This means the time to configure pins grows directly with the number of pins.

Common Mistake

[X] Wrong: "Configuring pins is always constant time no matter how many pins."

[OK] Correct: Each pin needs its own setup step, so more pins mean more time.

Interview Connect

Understanding how loops affect time helps you explain how embedded code scales with hardware changes.

Self-Check

"What if we configure pins in pairs instead of one by one? How would the time complexity change?"