⚡️ Speed up function FFT
by 102%
#104
Open
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.
📄 102% (1.02x) speedup for
FFT
insrc/numpy_pandas/signal_processing.py
⏱️ Runtime :
18.2 milliseconds
→9.04 milliseconds
(best of311
runs)📝 Explanation and details
The optimized code implements an iterative FFT algorithm that replaces the recursive Cooley-Tukey approach with a more cache-efficient, bottom-up computation strategy.
Key optimizations:
Eliminated recursion overhead: The original recursive implementation created deep call stacks and repeatedly sliced arrays (
x[0::2]
,x[1::2]
), causing significant memory allocation and copying. The optimized version uses an iterative approach that processes the FFT in-place.Bit-reversal preprocessing: Instead of recursively splitting arrays, the optimized version pre-computes the bit-reversed indices using efficient bitwise operations. This eliminates the need for array slicing entirely and arranges input data in the correct order for the iterative algorithm.
Reduced twiddle factor computation: The original code computed
np.exp(-2j * np.pi * np.arange(n) / n)
for every recursive call. The optimized version computes twiddle factors only once (np.exp(-2j * np.pi * np.arange(n // 2) / n)
) and reuses them throughout the iterative process.In-place butterfly operations: The iterative algorithm performs FFT butterfly operations directly on the result array, avoiding temporary array allocations that occurred in the recursive approach.
Performance characteristics by input size:
The optimization is most effective for large power-of-2 inputs where the recursive overhead and memory allocation costs dominate the computation time.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-FFT-mfelmm2r
and push.