⚡️ Speed up function _flatten_action_profile
by 10%
#6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 10% (0.10x) speedup for
_flatten_action_profile
inquantecon/game_theory/mclennan_tourky.py
⏱️ Runtime :
377 microseconds
→342 microseconds
(best of456
runs)📝 Explanation and details
The optimization eliminates the expensive
pure2mixed
function call for pure actions by inlining the conversion directly in_flatten_action_profile
.Key changes:
pure2mixed(num_actions, action_profile[i])
for pure actions, the code now directly sets the output array slice to zero and then sets the specific action index to 1.mixed_action
array viapure2mixed
, then copied it to the output. The optimized version writes directly to the output array.Why this is faster:
pure2mixed
)Performance characteristics:
The optimization shows the best gains for test cases with many pure actions (13.5-21.6% speedup), while mixed-action-only cases see minimal change (0-3% improvement). This makes sense since mixed actions still follow the same code path, but pure actions now avoid the expensive function call entirely.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_flatten_action_profile-mgg0c4ej
and push.