Why tasks are the building blocks in FreeRTOS - Performance Analysis
When working with FreeRTOS, tasks are the main units that do work. Understanding how time grows with tasks helps us see how system speed changes.
We want to know how adding more tasks affects how long the system takes to run.
Analyze the time complexity of the following code snippet.
// Create multiple tasks
for(int i = 0; i < numTasks; i++) {
xTaskCreate(
TaskFunction, // Task function
"Task", // Name
1000, // Stack size
NULL, // Parameters
1, // Priority
NULL // Task handle
);
}
This code creates a number of tasks one by one, each running the same function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop that calls
xTaskCreaterepeatedly. - How many times: Exactly
numTaskstimes, once per task.
Each new task adds one more call to create it, so the total work grows steadily as tasks increase.
| Input Size (numTasks) | Approx. Operations |
|---|---|
| 10 | 10 calls to create tasks |
| 100 | 100 calls to create tasks |
| 1000 | 1000 calls to create tasks |
Pattern observation: The work grows directly in step with the number of tasks.
Time Complexity: O(n)
This means if you double the number of tasks, the time to create them roughly doubles too.
[X] Wrong: "Creating many tasks happens instantly and does not add time."
[OK] Correct: Each task creation takes some time, so more tasks mean more total time spent creating them.
Knowing how task creation time grows helps you design systems that stay fast and responsive as they get bigger. This skill shows you understand how FreeRTOS manages work.
"What if we created tasks in parallel instead of a loop? How would the time complexity change?"