0
0
Embedded Cprogramming~5 mins

Bare-metal vs RTOS execution model in Embedded C - Performance Comparison

Choose your learning style9 modes available
Time Complexity: Bare-metal vs RTOS execution model
O(n)
Understanding Time Complexity

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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of tasks grows, the time to complete one loop cycle grows too.

Number of Tasks (n)Approx. Operations per Loop
33 task calls
1010 task calls
100100 task calls

Pattern observation: The time to finish one loop grows roughly in direct proportion to the number of tasks.

Final Time Complexity

Time Complexity: O(n)

This means the time to run all tasks once grows linearly as you add more tasks.

Common Mistake

[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.

Interview Connect

Understanding how task execution time grows helps you explain embedded system design choices clearly and confidently.

Self-Check

What if the RTOS used priority-based preemption instead of checking all tasks each loop? How would the time complexity change?