Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace numpyfft with scipyfft for better performance #892

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

@Sauravroy34
Copy link
Contributor Author

1_qKnX4-MhBzj5LL3thLXW6g

@Sauravroy34
Copy link
Contributor Author

compared to other avialble options numpy fft and scipy fft seem to have higher accuracy as well

@Sauravroy34 Sauravroy34 changed the title replaced numpyfft with scipyfft for better performance replace numpyfft with scipyfft for better performance Feb 26, 2025
@matteobachetti
Copy link
Member

@Sauravroy34 interesting, thanks. Do you have measurements for the 1D transform we use as well?

@Sauravroy34
Copy link
Contributor Author

Sauravroy34 commented Feb 26, 2025

sure , i could not find data for 1 d in internet so i decided of making one

import numpy as np
import scipy.fft
import timeit
import matplotlib.pyplot as plt

# Different sizes of input signals
sizes = [2**i for i in range(5, 21)]  
import pyfftw
def benchmark_fft(lib, size):
    x = np.random.random(size)
    if lib == 'numpy':
        return timeit.timeit(lambda: pyfftw.interfaces.numpy_fft.fft(x), number=10) / 10
    elif lib == 'scipy':
        return timeit.timeit(lambda: pyfftw.interfaces.scipy_fft.fft(x), number=10) / 10

numpy_times = [benchmark_fft('numpy', size) for size in sizes]
scipy_times = [benchmark_fft('scipy', size) for size in sizes]


plt.figure(figsize=(10, 6))
plt.plot(sizes, numpy_times, label='pyfftw NumPy FFT', marker='o')
plt.plot(sizes, scipy_times, label='pyfftw SciPy FFT', marker='s')
plt.xlabel('Input Size')
plt.ylabel('Time (seconds)')
plt.xscale('log')
plt.yscale('log')
plt.legend()
plt.title('Comparison of FFT Computation Time: NumPy vs SciPy')
plt.grid(True, which='both', linestyle='--', linewidth=0.5)
plt.show()

using pyfftw interfaces

output

@Sauravroy34
Copy link
Contributor Author

import numpy as np
import scipy.fft
import timeit
import matplotlib.pyplot as plt

# Different sizes of input signals
sizes = [2**i for i in range(5, 21)]  

def benchmark_fft(lib, size):
    x = np.random.random(size)
    if lib == 'numpy':
        return timeit.timeit(lambda: np.fft.fft(x), number=10) / 10
    elif lib == 'scipy':
        return timeit.timeit(lambda: scipy.fft.fft(x), number=10) / 10

numpy_times = [benchmark_fft('numpy', size) for size in sizes]
scipy_times = [benchmark_fft('scipy', size) for size in sizes]


plt.figure(figsize=(10, 6))
plt.plot(sizes, numpy_times, label='NumPy FFT', marker='o')
plt.plot(sizes, scipy_times, label='SciPy FFT', marker='s')
plt.xlabel('Input Size')
plt.ylabel('Time (seconds)')
plt.xscale('log')
plt.yscale('log')
plt.legend()
plt.title('Comparison of FFT Computation Time: NumPy vs SciPy')
plt.grid(True, which='both', linestyle='--', linewidth=0.5)
plt.show()

normal scipy fft and numpy fft

raw2

@matteobachetti
Copy link
Member

@Sauravroy34 would you mind re-running the benchmark inverting the order of the calls to numpy and scipy?

Just as a sanity check

@Sauravroy34
Copy link
Contributor Author

@Sauravroy34 would you mind re-running the benchmark inverting the order of the calls to numpy and scipy?

Just as a sanity check

inverting the calls seems to have reverse effect on plots it might be due to some caching effect i will try to create a uniform benchmark for both of them and post update here

@Sauravroy34
Copy link
Contributor Author

@matteobachetti in this comment the benchmark was conducted
#369 (comment)
Screenshot from 2025-02-26 20-15-13
Screenshot from 2025-02-26 20-15-29

@Sauravroy34
Copy link
Contributor Author

i have conducted few test myself here is the notebook https://github.com/Sauravroy34/Benchmark_test_for_scipy_and_numpy

new

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants