Register storage class - Time & Space 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.
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 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.
As n grows, the loop runs more times, so the total work grows in a straight line with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows evenly as n grows; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run grows directly in proportion to the input size.
[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.
Understanding how storage classes affect performance helps you explain code efficiency clearly and confidently.
"What if we replaced the register variable with a normal variable? How would the time complexity change?"