⚡️ Speed up function get_majority_class by 27%
#628
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.
📄 27% (0.27x) speedup for
get_majority_classininference/core/workflows/core_steps/fusion/detections_consensus/v1.py⏱️ Runtime :
1.38 milliseconds→1.08 milliseconds(best of396runs)📝 Explanation and details
The optimization replaces a list comprehension with
mapandzipfunctions to eliminate intermediate list creation. Instead of building a complete list of tuples in memory with[(str(class_name), int(class_id)) for ...], the optimized version useszip(map(str, detections["class_name"]), map(int, detections.class_id))which creates an iterator that generates tuples on-demand.Key changes:
map()to applystr()andint()conversions lazilyCounterconsumes the iterator directly without materializing the full listWhy this is faster:
Performance benefits by test case size:
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
workflows/unit_tests/core_steps/fusion/test_detections_consensus.py::test_get_majority_class🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_majority_class-mhbv13caand push.