Generated C code inspection in Simulink - Time & Space Complexity
When we inspect generated C code from Simulink models, we want to know how the code's running time changes as the input size grows.
We ask: How does the code's work increase when inputs get bigger?
Analyze the time complexity of the following generated C code snippet.
// Generated C code from Simulink
void processData(double input[], int size, double output[]) {
int i;
for (i = 0; i < size; i++) {
output[i] = input[i] * 2.0;
}
}
This code doubles each element in an input array and stores it in an output array.
Look for loops or repeated steps.
- Primary operation: The for-loop that multiplies each element by 2.
- How many times: The loop runs once for each element in the input array, so size times.
As the input array gets bigger, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 multiplications |
| 100 | 100 multiplications |
| 1000 | 1000 multiplications |
Pattern observation: The work grows directly with the input size. Double the input, double the work.
Time Complexity: O(n)
This means the time to run the code grows in a straight line with the number of input elements.
[X] Wrong: "The loop runs a fixed number of times, so the time is constant."
[OK] Correct: The loop count depends on the input size, so if the input grows, the loop runs more times, increasing the total work.
Understanding how generated code scales helps you explain performance clearly and shows you can read and analyze real code efficiently.
"What if the code had a nested loop inside the for-loop that also ran 'size' times? How would the time complexity change?"