Polling vs interrupt-driven execution in Embedded C - Performance Comparison
We want to understand how the time cost changes when using polling or interrupts in embedded C programs.
Which method uses more CPU time as input or events increase?
Analyze the time complexity of the following code snippet.
// Polling example
while(1) {
if (data_ready()) {
process_data();
}
}
// Interrupt example
void ISR() {
process_data();
}
This code shows a polling loop checking for data readiness continuously, and an interrupt service routine (ISR) that runs only when data is ready.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: In polling, the loop repeatedly checks data readiness.
- How many times: The check runs continuously, many times even if no data is ready.
- Interrupt-driven: The process_data() runs only when an interrupt occurs, not repeatedly.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Polling: many checks per second; Interrupt: about 10 process_data calls |
| 100 | Polling: still many checks per second; Interrupt: about 100 process_data calls |
| 1000 | Polling: many checks per second; Interrupt: about 1000 process_data calls |
Pattern observation: Polling does many checks regardless of input size, while interrupts run process_data only when needed.
Time Complexity: O(n) for interrupt-driven, O(1) per loop iteration but repeated constantly for polling.
Interrupt-driven execution scales with actual events, polling wastes time checking repeatedly.
[X] Wrong: "Polling is always faster because it checks continuously."
[OK] Correct: Polling wastes CPU time checking even when no data is ready, making it inefficient as input grows.
Understanding how polling and interrupts affect program speed and CPU use is a key skill for embedded programming and real-time systems.
"What if the polling loop included a delay? How would that change the time complexity and CPU usage?"
Practice
Solution
Step 1: Understand polling concept
Polling means the program continuously checks if an event has happened by running a loop.Step 2: Compare options
Polling repeatedly checks for an event in a loop. correctly states polling as repeated checking. Options A, B, and C describe interrupts or incorrect facts.Final Answer:
Polling repeatedly checks for an event in a loop. -> Option AQuick Check:
Polling = repeated checking [OK]
- Confusing polling with interrupts
- Thinking polling is automatic
- Assuming polling disables interrupts
Solution
Step 1: Recall ISR declaration syntax
In many embedded C compilers, the keyword __interrupt before the function declares it as an ISR.Step 2: Check options
__interrupt void ISR() { /* code */ } uses __interrupt correctly. Options A, B, and D use incorrect or incomplete syntax.Final Answer:
__interrupt void ISR() { /* code */ } -> Option BQuick Check:
ISR declaration = __interrupt keyword [OK]
- Omitting __interrupt keyword
- Placing interrupt keyword after void
- Using plain void without interrupt
while(1) {
if (button_pressed()) {
toggle_led();
}
}
What happens when the button is pressed?Solution
Step 1: Analyze the polling loop
The loop continuously checks if button_pressed() returns true, then calls toggle_led() immediately.Step 2: Understand behavior on button press
When the button is pressed, button_pressed() becomes true, so toggle_led() runs right away.Final Answer:
The LED toggles immediately when the button is pressed. -> Option CQuick Check:
Polling triggers action immediately on event [OK]
- Thinking toggle waits for loop end
- Confusing polling with interrupts
- Assuming interrupts are required
void ISR() {
toggle_led();
}
int main() {
enable_interrupts();
while(1) {}
}
What is the main problem?Solution
Step 1: Check ISR declaration
The ISR function lacks the __interrupt keyword, so it won't be recognized as an interrupt handler.Step 2: Review other code parts
enable_interrupts() is correctly called once; toggle_led() can be called in ISR; infinite loop is normal in embedded main.Final Answer:
ISR is missing the __interrupt keyword. -> Option DQuick Check:
ISR needs __interrupt keyword [OK]
- Forgetting __interrupt keyword
- Thinking infinite loop is wrong
- Assuming toggle_led() is invalid in ISR
Solution
Step 1: Understand the requirement
The program should react only when the sensor changes and avoid wasting CPU time on constant checking.Step 2: Compare methods
Polling (options B, C, D) wastes CPU by checking repeatedly. Interrupt (Use an interrupt triggered by the sensor change.) triggers only on change, saving CPU.Final Answer:
Use an interrupt triggered by the sensor change. -> Option AQuick Check:
Interrupts save CPU by reacting only on events [OK]
- Choosing polling for efficiency
- Using delays wastes CPU
- Confusing timer polling with interrupts
Ready to prove it?
Pass the test and this pattern moves unknown
