0
0
Embedded Cprogramming~5 mins

Reading a hardware register in Embedded C - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Reading a hardware register
O(n)
Understanding Time Complexity

When reading a hardware register, we want to know how the time to read changes as we do more reads.

We ask: How does the work grow if we read the register many times?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


volatile uint32_t *REG = (uint32_t *)0x40000000;
uint32_t read_value;

for (int i = 0; i < n; i++) {
    read_value = *REG;
}
    

This code reads a hardware register at a fixed address n times in a loop.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Reading the hardware register once per loop.
  • How many times: The read happens n times, once each loop cycle.
How Execution Grows With Input

Each time we increase n, the number of reads grows the same amount.

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

Pattern observation: The work grows directly with n. Double n, double the reads.

Final Time Complexity

Time Complexity: O(n)

This means the time to complete grows in a straight line as you read more times.

Common Mistake

[X] Wrong: "Reading a hardware register is instant and does not add time."

[OK] Correct: Each read takes some time, so doing many reads adds up and grows with n.

Interview Connect

Understanding how repeated hardware reads affect time helps you reason about performance in embedded systems.

Self-Check

"What if we added a delay inside the loop after each read? How would the time complexity change?"