Why branching controls program execution in ARM Architecture - Performance Analysis
Branching changes which instructions run next in a program. Understanding its time cost helps us see how programs behave as they make decisions.
We ask: How does branching affect the number of steps a program takes?
Analyze the time complexity of the following code snippet.
CMP R0, #0
BEQ label_zero
ADD R1, R1, #1
B end
label_zero:
SUB R1, R1, #1
end:
MOV R2, R1
This code compares a value and branches to different instructions based on the result, controlling which operations run.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single conditional branch deciding which instruction to run next.
- How many times: This snippet runs once per execution; no loops or repeated branches here.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | About 5 instructions |
| 10 | About 50 instructions if repeated 10 times |
| 100 | About 500 instructions if repeated 100 times |
Pattern observation: Each run executes a fixed number of steps; branching only changes which steps run, not how many.
Time Complexity: O(1)
This means the number of steps stays the same no matter the input; branching just picks the path.
[X] Wrong: "Branching always makes the program slower because it adds extra steps."
[OK] Correct: Branching only chooses which instructions run; it does not add extra repeated work by itself.
Understanding how branching affects program steps helps you explain decision-making in code clearly and confidently.
"What if the branch was inside a loop running n times? How would the time complexity change?"