⚡️ Speed up function get_average_bounding_box by 14%
          #629
        
          
      
  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.
  
    
  
    
📄 14% (0.14x) speedup for
get_average_bounding_boxininference/core/workflows/core_steps/fusion/detections_consensus/v1.py⏱️ Runtime :
2.61 milliseconds→2.29 milliseconds(best of403runs)📝 Explanation and details
The optimized code replaces
np.mean()with a manual calculation usingnp.add.reduce()followed by division. This achieves a 14% speedup by eliminating the overhead of NumPy's mean function.Key optimization:
np.mean(detections.xyxy, axis=0)tonp.add.reduce(detections.xyxy, axis=0) / len(detections)Why this is faster:
np.mean()internally performs additional operations like handling NaN values, dtype validation, and other statistical computations. By usingnp.add.reduce()(which efficiently sums along an axis) and manually dividing by the length, we bypass this overhead and perform only the essential mathematical operations needed for averaging.Performance characteristics:
This optimization is particularly effective for typical computer vision workloads where bounding box averaging involves small to medium-sized detection sets, making it ideal for real-time inference scenarios.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
workflows/unit_tests/core_steps/fusion/test_detections_consensus.py::test_get_average_bounding_box_when_multiple_elements_providedworkflows/unit_tests/core_steps/fusion/test_detections_consensus.py::test_get_average_bounding_box_when_single_element_provided🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_average_bounding_box-mhbv90i8and push.