⚡️ Speed up function periodogram by 51%#120
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
Conversation
The optimized code achieves a **50% speedup** by replacing the complex FFT (`fft`) with the real FFT (`rfft`) for real-valued inputs in the `periodogram()` function. ## Key Optimization **What changed:** Added a conditional check using `np.isrealobj(x)` to determine if the input is real-valued. When true, it uses `rfft()` instead of `fft()`. **Why it's faster:** - `fft()` computes the full complex FFT with N output points, then the code slices to keep only the first `n//2 + 1` points (the positive frequencies). - `rfft()` exploits the symmetry property of real signals (where negative frequencies are conjugates of positive ones) and directly computes only the `n//2 + 1` non-redundant frequency bins. - This eliminates ~50% of the FFT computation work for real inputs. **Line profiler evidence:** The FFT computation time dropped from **7.31ms to 4.94ms** (32% reduction), which accounts for most of the overall speedup. The `rfft` line takes 59% of total time vs 73.3% for the original `fft` line, showing improved efficiency despite still being the dominant operation. ## Performance Characteristics **Test results show:** - Negligible impact on small arrays (< 50 elements): Most tests show ±1-3% variation within measurement noise - **Best gains on larger arrays:** The `test_large_scale_periodogram` (512 elements) and `test_edge_power_of_two` (varying power-of-2 sizes) show 4-7% improvements, suggesting the optimization scales with input size - Minimal overhead from the `np.isrealobj()` check (~0.7μs based on profiler data) ## Impact on Workloads Based on `function_references`, the `periodogram()` function is called by `ar_periodogram()` which performs autoregressive spectral estimation. Since this is a computational analysis function likely called on signal processing data: - **Real-world benefit:** Most time-series and signal data are real-valued, so the `rfft` path will be taken in typical usage - **AR modeling workflow:** The function is called after computing residuals from AR(1) regression, which produces real-valued residuals, making this optimization highly relevant for the documented use case The optimization maintains identical output for real inputs (both produce the same positive-frequency spectrum) while providing automatic acceleration without API changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 51% (0.51x) speedup for
periodograminquantecon/_estspec.py⏱️ Runtime :
6.07 milliseconds→4.03 milliseconds(best of91runs)📝 Explanation and details
The optimized code achieves a 50% speedup by replacing the complex FFT (
fft) with the real FFT (rfft) for real-valued inputs in theperiodogram()function.Key Optimization
What changed: Added a conditional check using
np.isrealobj(x)to determine if the input is real-valued. When true, it usesrfft()instead offft().Why it's faster:
fft()computes the full complex FFT with N output points, then the code slices to keep only the firstn//2 + 1points (the positive frequencies).rfft()exploits the symmetry property of real signals (where negative frequencies are conjugates of positive ones) and directly computes only then//2 + 1non-redundant frequency bins.Line profiler evidence: The FFT computation time dropped from 7.31ms to 4.94ms (32% reduction), which accounts for most of the overall speedup. The
rfftline takes 59% of total time vs 73.3% for the originalfftline, showing improved efficiency despite still being the dominant operation.Performance Characteristics
Test results show:
test_large_scale_periodogram(512 elements) andtest_edge_power_of_two(varying power-of-2 sizes) show 4-7% improvements, suggesting the optimization scales with input sizenp.isrealobj()check (~0.7μs based on profiler data)Impact on Workloads
Based on
function_references, theperiodogram()function is called byar_periodogram()which performs autoregressive spectral estimation. Since this is a computational analysis function likely called on signal processing data:rfftpath will be taken in typical usageThe optimization maintains identical output for real inputs (both produce the same positive-frequency spectrum) while providing automatic acceleration without API changes.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-periodogram-mkp8pmqhand push.