⚡️ Speed up function _get_action_profile by 20%
#5
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.
📄 20% (0.20x) speedup for
_get_action_profileinquantecon/game_theory/mclennan_tourky.py⏱️ Runtime :
310 microseconds→259 microseconds(best of199runs)📝 Explanation and details
The optimized code achieves a 19% speedup by replacing the generator expression with a more efficient two-step approach using
zip()and list comprehension.Key optimization: Instead of
tuple(x[indptr[i]:indptr[i+1]] for i in range(N)), the code uses:zip(indptr[:-1], indptr[1:])to pair consecutive indices without explicit range iteration[x[start:end] for start, end in ...]followed bytuple(sliced)Why this is faster:
indptr[i]andindptr[i+1]lookups in each iteration. The optimized version pre-computes these pairs withzip(), reducing attribute access overhead.tuple(), as it avoids the generator state machine.zip()operation creates consecutive start/end pairs that are processed sequentially.Test case performance patterns:
zip()overhead, but this is negligible compared to the gains on realistic workloadsThe optimization is particularly effective for game theory applications with many players or complex action spaces, which are the primary use cases for this function.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_get_action_profile-mgg06jygand push.