Wake-up sources configuration in Embedded C - Time & Space Complexity
When configuring wake-up sources in embedded systems, it's important to know how the time to set them up changes as the number of sources grows.
We want to understand how the setup time scales when more wake-up sources are added.
Analyze the time complexity of the following code snippet.
void configureWakeupSources(int *sources, int count) {
for (int i = 0; i < count; i++) {
enableWakeupSource(sources[i]);
}
}
// Enables a single wake-up source
void enableWakeupSource(int source) {
// Hardware register setup here
}
This code sets up each wake-up source one by one by calling a function for each source.
- Primary operation: Loop over the array of wake-up sources.
- How many times: Exactly once for each source, so
counttimes.
As the number of wake-up sources increases, the time to configure them grows in a straight line.
| Input Size (count) | Approx. Operations |
|---|---|
| 10 | 10 calls to enableWakeupSource |
| 100 | 100 calls to enableWakeupSource |
| 1000 | 1000 calls to enableWakeupSource |
Pattern observation: The setup time grows directly with the number of sources; doubling sources doubles the work.
Time Complexity: O(n)
This means the time to configure wake-up sources grows linearly with how many sources you have.
[X] Wrong: "Configuring multiple wake-up sources takes the same time no matter how many there are."
[OK] Correct: Each source requires a separate setup step, so more sources mean more work and more time.
Understanding how setup time grows with input size helps you explain system behavior clearly and shows you can think about efficiency in embedded programming.
"What if we batch configure all wake-up sources in one hardware call instead of individually? How would the time complexity change?"