⚡️ Speed up method PolymatrixGame.range_of_payoffs by 1,474%#28
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up method PolymatrixGame.range_of_payoffs by 1,474%#28codeflash-ai[bot] wants to merge 1 commit intomainfrom
PolymatrixGame.range_of_payoffs by 1,474%#28codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimization replaces NumPy's `np.min()` and `np.max()` operations with a custom Numba-compiled function `_matrix_min_max()`, achieving a **14.7x speedup** (from 68.8ms to 4.37ms). **Key Optimization:** - **Numba JIT compilation**: The `@njit(cache=True)` decorator compiles `_matrix_min_max()` to native machine code, eliminating Python overhead for the inner loops - **Single-pass min/max**: Instead of making two separate passes through each matrix (one for min, one for max), the optimized version finds both values in a single traversal - **Reduced NumPy function call overhead**: Direct element access replaces NumPy's vectorized operations, which have overhead for smaller matrices **How it works:** The original code calls `np.min()` and `np.max()` on each matrix separately, creating temporary arrays and invoking NumPy's C routines with Python overhead. The optimized version uses a simple nested loop in compiled code that processes each element once, tracking both minimum and maximum values simultaneously. **Performance characteristics:** - **Best for small-to-medium matrices**: Test results show 4-11x speedups for typical game theory matrices (2x2 to 100x100) - **Compilation cost**: The first call incurs JIT compilation overhead, but subsequent calls benefit from cached compilation - **Memory efficiency**: Eliminates intermediate arrays created by separate min/max operations **Trade-offs:** - The large sparse test case (20 players, 20x20 matrices) shows the most dramatic improvement (7.6x speedup), while the random large matrix test shows slight regression (-28.6%), indicating the optimization is most effective for structured data patterns typical in game theory applications. This optimization is particularly valuable for polymatrix games where `range_of_payoffs()` may be called repeatedly during equilibrium analysis or strategy evaluation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 1,474% (14.74x) speedup for
PolymatrixGame.range_of_payoffsinquantecon/game_theory/polymatrix_game.py⏱️ Runtime :
68.8 milliseconds→4.37 milliseconds(best of59runs)📝 Explanation and details
The optimization replaces NumPy's
np.min()andnp.max()operations with a custom Numba-compiled function_matrix_min_max(), achieving a 14.7x speedup (from 68.8ms to 4.37ms).Key Optimization:
@njit(cache=True)decorator compiles_matrix_min_max()to native machine code, eliminating Python overhead for the inner loopsHow it works:
The original code calls
np.min()andnp.max()on each matrix separately, creating temporary arrays and invoking NumPy's C routines with Python overhead. The optimized version uses a simple nested loop in compiled code that processes each element once, tracking both minimum and maximum values simultaneously.Performance characteristics:
Trade-offs:
This optimization is particularly valuable for polymatrix games where
range_of_payoffs()may be called repeatedly during equilibrium analysis or strategy evaluation.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-PolymatrixGame.range_of_payoffs-mi9kwdszand push.