⚡️ Speed up function close_normal_form_games by 371%
#39
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.
📄 371% (3.71x) speedup for
close_normal_form_gamesinquantecon/game_theory/tests/test_polymatrix_game.py⏱️ Runtime :
2.95 milliseconds→627 microseconds(best of164runs)📝 Explanation and details
The optimized version achieves a 371% speedup by replacing Python loops and NumPy's
allclosefunction with faster Numba-compiled alternatives for the most computationally expensive operations.Key Optimizations Applied:
Numba-compiled action comparison (
_nums_actions_equal): Replaces the Python loop that comparesnums_actionsarrays with a JIT-compiled function, eliminating Python interpreter overhead for this comparison.Custom Numba array comparison (
_allclose_ndarray): Replaces NumPy'sallclosefunction with a specialized Numba implementation that directly iterates over flattened arrays, avoiding NumPy's overhead for tolerance checking.Smart fallback strategy: The code maintains full compatibility by falling back to the original NumPy logic for edge cases (empty games, shape mismatches) while using the optimized path for common scenarios.
Why This Creates Significant Speedup:
allclosecalls consumed 98% of the original runtime. The Numba version compiles to native machine code, removing Python's interpreted loop overhead.allclose, the optimized version uses a specialized comparison that's tailored for this specific use case.@njit(cache=True)decorator ensures the compiled functions are cached, making subsequent calls even faster.Performance Impact on Workloads:
Based on the function references,
close_normal_form_gamesis called in test scenarios that involve:PolymatrixGame.from_nf,polymg.to_nfg)The test results show consistent 2-4x speedups across various scenarios, with the largest gains (up to 16x) occurring when arrays differ early in the comparison, allowing for fast rejection.
Best Performance Gains For:
The optimization is particularly valuable in iterative algorithms or batch processing scenarios where game comparisons are performed repeatedly.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-close_normal_form_games-mj9prtk2and push.