vTaskPrioritySet() dynamic priority in FreeRTOS - Time & Space Complexity
When changing a task's priority dynamically in FreeRTOS, it's important to know how the time taken grows as the system has more tasks.
We want to understand how the cost of setting a new priority changes with the number of tasks.
Analyze the time complexity of the following code snippet.
// Change the priority of a task dynamically
void changeTaskPriority(TaskHandle_t task, UBaseType_t newPriority) {
vTaskPrioritySet(task, newPriority);
}
This code changes the priority of a given task to a new level during runtime.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The function internally may check the ready lists for tasks at different priorities.
- How many times: It scans through priority levels, which depends on the total number of priority levels configured.
Changing a task's priority involves checking priority lists. The time grows with the number of priority levels, not the number of tasks.
| Input Size (priority levels) | Approx. Operations |
|---|---|
| 5 | 5 checks |
| 10 | 10 checks |
| 20 | 20 checks |
Pattern observation: The cost grows linearly with the number of priority levels, not tasks.
Time Complexity: O(P)
This means the time to set a task's priority grows linearly with the number of priority levels configured in the system.
[X] Wrong: "Changing a task's priority takes time proportional to the total number of tasks in the system."
[OK] Correct: The operation depends on the number of priority levels, which is usually fixed and small, not on how many tasks exist.
Understanding how task priority changes scale helps you reason about real-time system responsiveness and scheduling efficiency.
"What if the system had a very large number of priority levels? How would that affect the time complexity of vTaskPrioritySet()?"