⚡️ Speed up function _rank_population by 5%
#30
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.
📄 5% (0.05x) speedup for
_rank_populationinoptuna/samplers/nsgaii/_elite_population_selection_strategy.py⏱️ Runtime :
12.2 milliseconds→11.6 milliseconds(best of8runs)📝 Explanation and details
The optimized code achieves a 5% speedup through several key optimizations:
1. List Comprehension in
_evaluate_penalty(constraints := trial.system_attrs.get(_CONSTRAINTS_KEY))2. Bitwise Operations in
_fast_non_domination_ranknp.logical_andto bitwise operators:(~is_penalty_nan) & (penalty <= 0)3. Conditional Execution Guards
if feasible_count:,if infeasible_count:, andif penalty_nan_count:checks_calculate_nondomination_rankwhen no trials exist in a category4. Optimized Array Size Calculation
max(domination_ranks) + 1withdomination_ranks.max() + 1in_rank_population.max()method is faster than Python's built-inmax()function for arraysPerformance Impact by Test Case:
The optimizations are particularly effective for scenarios with large populations and constrained optimization problems, where the reduced function call overhead and vectorized operations provide the most benefit.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
samplers_tests/test_nsgaii.py::test_rank_population_emptysamplers_tests/test_nsgaii.py::test_rank_population_missing_constraint_valuessamplers_tests/test_nsgaii.py::test_rank_population_no_constraintssamplers_tests/test_nsgaii.py::test_rank_population_with_constraints🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_rank_population-mhaypqbband push.