0
0
Cprogramming~5 mins

Register storage class - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Register storage class
O(n)
Understanding Time Complexity

Let's see how using the register storage class affects how fast a program runs.

We want to know if it changes how the program's speed grows when input gets bigger.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


void countToN(int n) {
    register int i;
    for (i = 0; i < n; i++) {
        // do something simple
    }
}
    

This code counts from 0 up to n-1 using a register variable for the loop counter.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The for-loop runs a simple operation inside.
  • How many times: It runs exactly n times, where n is the input size.
How Execution Grows With Input

As n grows, the loop runs more times, so the total work grows in a straight line with n.

Input Size (n)Approx. Operations
1010
100100
10001000

Pattern observation: The work grows evenly as n grows; doubling n doubles the work.

Final Time Complexity

Time Complexity: O(n)

This means the time to run grows directly in proportion to the input size.

Common Mistake

[X] Wrong: "Using register makes the loop run faster and changes the time complexity."

[OK] Correct: Register may speed up access but does not change how many times the loop runs, so the overall growth stays the same.

Interview Connect

Understanding how storage classes affect performance helps you explain code efficiency clearly and confidently.

Self-Check

"What if we replaced the register variable with a normal variable? How would the time complexity change?"