⚡️ Speed up function histogram_equalization by 31,942%
#209
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.
📄 31,942% (319.42x) speedup for
histogram_equalizationinsrc/signal/image.py⏱️ Runtime :
2.26 seconds→7.05 milliseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 319x speedup by replacing nested Python loops with vectorized NumPy operations, which are executed in highly optimized C code.
Key Optimizations
1. Vectorized Histogram Computation (74.4% → 27.8% of runtime)
The original code used nested loops to build the histogram:
The optimized version uses
np.bincount():Why this is faster:
bincountis a compiled C function that directly counts occurrences in a single pass, eliminating Python loop overhead and individual array indexing operations.2. Vectorized Output Generation (74.4% → 13.6% of runtime)
The original code mapped each pixel individually:
The optimized version uses fancy indexing:
Why this is faster: Pre-computing the mapping table and using advanced indexing (
mapping[image]) allows NumPy to apply the transformation to all pixels in parallel, avoiding per-pixel Python interpretation.3. Preserved Behavior
IndexErrorbehavior for out-of-range valuesPerformance Impact by Test Case
The optimization is particularly effective for typical image processing workloads where images contain hundreds to millions of pixels, making nested loop elimination critical for performance.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-histogram_equalization-mji18domand push.