Bare-metal vs RTOS execution model in Embedded C - Performance Comparison
We want to understand how the execution time changes when using bare-metal code versus an RTOS in embedded C.
How does the program's running time grow as tasks or inputs increase?
Analyze the time complexity of these two simple execution models.
// Bare-metal loop
while(1) {
task1();
task2();
task3();
}
// RTOS task scheduler
void scheduler() {
while(1) {
if(task1_ready) run_task1();
if(task2_ready) run_task2();
if(task3_ready) run_task3();
}
}
This code shows a bare-metal infinite loop running tasks one after another, and an RTOS scheduler checking and running ready tasks.
Look at what repeats in each model.
- Primary operation: The infinite loop running tasks repeatedly.
- How many times: The loop runs forever, repeating all tasks each cycle.
As the number of tasks grows, the time to complete one loop cycle grows too.
| Number of Tasks (n) | Approx. Operations per Loop |
|---|---|
| 3 | 3 task calls |
| 10 | 10 task calls |
| 100 | 100 task calls |
Pattern observation: The time to finish one loop grows roughly in direct proportion to the number of tasks.
Time Complexity: O(n)
This means the time to run all tasks once grows linearly as you add more tasks.
[X] Wrong: "Adding an RTOS scheduler makes the execution time constant regardless of tasks."
[OK] Correct: The scheduler still checks each task, so time grows with the number of tasks, just like bare-metal.
Understanding how task execution time grows helps you explain embedded system design choices clearly and confidently.
What if the RTOS used priority-based preemption instead of checking all tasks each loop? How would the time complexity change?