⚡️ Speed up function _equilibrium_payoffs_abreu_sannikov by 20%
#63
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
_equilibrium_payoffs_abreu_sannikovinquantecon/game_theory/repeated_game.py⏱️ Runtime :
68.1 milliseconds→57.0 milliseconds(best of30runs)📝 Explanation and details
The optimized code achieves a 19% speedup by replacing non-JIT compiled functions with Numba-optimized versions, which is particularly beneficial for the computationally intensive loops in this repeated game algorithm.
Key Optimizations Applied:
JIT-compiled helper functions: Created
_compute_best_dev_gain_numba(),_R_numba(), and_update_u_numba()with@njit(cache=True)decorators to eliminate Python interpreter overhead in hot loops.Manual loop optimization in
_R_numba(): Replaced NumPy's vectorized operations like(action_profile_payoff >= IC).all()and(equations @ extended_payoff <= tol).all()with explicit loops that Numba can heavily optimize.Eliminated tuple comprehension in
_best_dev_gains(): The original code used a generator expression withnp.max()calls that couldn't be JIT-compiled. The optimized version uses a dedicated Numba function with manual loops for maximum performance.Why These Optimizations Work:
_R()function contains nested loops over all action pairs, making it the dominant bottleneck (89.5% of runtime in the original). Numba eliminates Python overhead and enables CPU-level optimizations.cache=Trueparameter avoids recompilation overhead across multiple calls, important since this function is called fromequilibrium_payoffs()which may be invoked repeatedly.Impact on Workloads:
Based on the function references,
_equilibrium_payoffs_abreu_sannikov()is called from the mainequilibrium_payoffs()method, making it a core computational path. The test results show consistent improvements across different game sizes:The optimization is particularly valuable for researchers running multiple equilibrium computations or analyzing larger game matrices, where the cumulative time savings become significant.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_equilibrium_payoffs_abreu_sannikov-mja8w7c1and push.