Entry, during, and exit actions in Simulink - Time & Space Complexity
We want to understand how the time cost changes when using entry, during, and exit actions in Simulink Stateflow charts.
How does the number of actions affect the total execution time as the model runs?
Analyze the time complexity of the following Stateflow actions.
state A {
entry: x = x + 1;
during: y = y + 2;
exit: z = z + 3;
}
while (condition) {
execute state A;
}
This code shows a state with entry, during, and exit actions executed repeatedly in a loop.
Look at what repeats as the model runs.
- Primary operation: The during action runs every cycle while the state is active.
- How many times: Entry runs once when entering, exit runs once when leaving, during runs every loop cycle.
As the number of loop cycles grows, the during action runs more times, while entry and exit run only once per state change.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Entry: 1, During: 10, Exit: 1 |
| 100 | Entry: 1, During: 100, Exit: 1 |
| 1000 | Entry: 1, During: 1000, Exit: 1 |
Pattern observation: The during action dominates and grows linearly with the number of cycles.
Time Complexity: O(n)
This means the total execution time grows linearly with the number of times the state runs its during action.
[X] Wrong: "Entry and exit actions run every cycle just like during actions."
[OK] Correct: Entry and exit actions run only once per state transition, so they do not add up with each cycle like during actions do.
Understanding how different actions affect execution time helps you design efficient state machines and explain your reasoning clearly in discussions.
"What if the during action contained a nested loop that runs m times each cycle? How would the time complexity change?"
Practice
entry action in a state?Solution
Step 1: Understand what entry action means
An entry action runs only once when the state becomes active, right at the moment of entry.Step 2: Compare with other actions
During actions run continuously while in the state, exit actions run once when leaving the state, and transitions define movement between states.Final Answer:
To execute code once when the state is entered -> Option BQuick Check:
Entry action = runs once on entering state [OK]
- Confusing entry with during actions
- Thinking entry runs multiple times
- Mixing entry with exit actions
Idle?Solution
Step 1: Recall exit action syntax in Stateflow
Exit actions are defined with the keywordexit:followed by the action code.Step 2: Check each option
exit: disp('Exiting Idle'); uses correctexit:syntax. on exit: disp('Exiting Idle'); uses incorrecton exit:. exit() { disp('Exiting Idle'); } uses function style which is invalid. exit_action: disp('Exiting Idle'); usesexit_action:which is not recognized.Final Answer:
exit: disp('Exiting Idle'); -> Option CQuick Check:
Exit action syntax = exit: code [OK]
- Using 'on exit:' instead of 'exit:'
- Trying to define exit as a function
- Using incorrect keywords like 'exit_action:'
Processing with these actions:entry: count = 0; during: count = count + 1; exit: disp(count);
What will be displayed when the state
Processing is exited after being active for 3 simulation steps?Solution
Step 1: Track variable changes during state
On entry,countis set to 0. During each step, it increments by 1. After 3 steps, count = 3.Step 2: Determine exit action output
On exit,disp(count)prints the current count, which is 3.Final Answer:
3 -> Option AQuick Check:
Count increments 3 times, exit prints 3 [OK]
- Forgetting entry sets count to 0
- Assuming exit prints 0 or 1
- Counting one extra increment
entry: x = 5; during: x = x + 2; exit: disp(x);
But when you run the simulation,
disp(x) shows 5 instead of increasing values. What is the likely error?Solution
Step 1: Analyze why during action might not run
If the state is active only one step or immediately exited, the during action does not get a chance to incrementx.Step 2: Check other options for correctness
Entry runs once on entry, not every step. Exit action placement is correct. Variable declaration is not required to see this behavior.Final Answer:
The during action is not executing because the state is not active long enough -> Option AQuick Check:
During runs only while state active [OK]
- Thinking entry runs every step
- Misplacing exit action inside during
- Assuming variable declaration causes issue
Active is entered during a simulation. Which approach correctly uses entry, during, and exit actions to achieve this?Solution
Step 1: Understand counting entries
To count entries, incrementcountonce each time the state is entered. This is done in the entry action.Step 2: Evaluate other options
During increments continuously, not suitable for counting entries. Exit increments on leaving, not entering. Resetting count on entry loses previous counts.Final Answer:
Use entry: count = count + 1; and initialize count outside the state; no during or exit needed -> Option DQuick Check:
Entry action increments count once per entry [OK]
- Incrementing count in during action
- Resetting count on every entry
- Incrementing count on exit instead of entry
Ready to prove it?
Pass the test and this pattern moves unknown
