def benchmark_2d_mfts(max_pow=13, savefig=False): """Benchmark MFT runtime vs array size, for multiple transform libraries Parameters ---------- mode : string, 'poppy' or 'base' What to test, either the full poppy usage of the 2D transform, including surrounding setup and normalization code, or the basic transform on its own. max_pow : int Maximum power of 2 array size to test up to savefig : bool save plot result to a PDF? """ # modified based on https://github.com/numpy/numpy/issues/17839#issuecomment-733543850 from simple_benchmark import benchmark import functools import poppy def shp(len): return (len, len) def test_mft_numpy(array, npix=64): global _USE_NUMEXPR _USE_NUMEXPR = False poppy.matrixDFT.matrix_dft(array, 16, npix) def test_mft_numexpr(array, npix=64): global _USE_NUMEXPR _USE_NUMEXPR = True poppy.matrixDFT.matrix_dft(array, 16, npix) test_mft_numpy_512 = functools.partial(test_mft_numpy, npix=512) test_mft_numexpr_512 = functools.partial(test_mft_numexpr, npix=512) b_array = benchmark( [test_mft_numpy, test_mft_numexpr, test_mft_numpy_512, test_mft_numexpr_512], arguments={2 ** i: np.random.uniform(size=shp(2 ** i)) + 1j * np.random.uniform(size=shp(2 ** i)) for i in range(2, max_pow)}, argument_name='pupil array size, npupil', function_aliases={test_mft_numpy: "MFT with numpy, npix=64", test_mft_numexpr: "MFT with numexpr, npix=64", test_mft_numpy_512: "MFT with numpy, npix=512", test_mft_numexpr_512: "MFT with numexpr, npix=512"} ) plt.figure(figsize=(12, 8)) b_array.plot() plt.grid(which='both', alpha=0.2) plt.grid(which='major', alpha=0.5) plt.xlim(1, 2e4) plt.ylim(1e-6, 1e1) cpu_label = get_processor_name() + f", {get_physical_cpu_count()} cores" plt.title(f"Matrix Fourier Transform timings\n{cpu_label}", fontweight='bold') if savefig: plt.savefig(f"bench_mfts.png")
def test_readme(): from simple_benchmark import benchmark import numpy as np funcs = [sum, np.sum] arguments = {i: [1] * i for i in [1, 10, 100, 1000, 10000, 100000]} argument_name = 'list size' aliases = {sum: 'Python sum', np.sum: 'NumPy sum'} b = benchmark(funcs, arguments, argument_name, function_aliases=aliases) b.to_pandas_dataframe() b.plot()
def test_extended_time_and_max(): from simple_benchmark import benchmark from datetime import timedelta def O_n(n): for i in range(n): pass def O_n_squared(n): for i in range(n**2): pass def O_n_cube(n): for i in range(n**3): pass b = benchmark([O_n, O_n_squared, O_n_cube], {2**i: 2**i for i in range(2, 15)}, time_per_benchmark=timedelta(milliseconds=500), maximum_time=timedelta(milliseconds=500)) b.plot()
res = [] for i in range(2, 6): res.append(x**i) return res def get_degree(x, de): if de == 2: return [x**de] else: res = get_degree(x, de - 1) res.append(x**de) return res def get_degree_r(x): return get_degree(x, 5) func = [get_degree_c, get_degree_r] arguments = {} for i in range(50): arguments['i' + str(i)] = i print(arguments) arguments_name = 'natural numbers' aliases = {get_degree_c: "Циклическая функция", get_degree_r: "Рекурсия"} b = benchmark(func, arguments, arguments_name, function_aliases=aliases) b.plot() plt.show(b)
from simple_benchmark import benchmark from factorial import fact_rec from cikl import cikl import matplotlib.pyplot as plt func = [fact_rec, cikl] # список с нашими функциями arguments = {} #у нас аргументы передаются в словаре for i in range(2, 30): arguments[str(i)] = i print(arguments) arguments_name = 'натуральные числа' aliases = {fact_rec: 'простая рекурсия', cikl: 'использование цикла'} b = benchmark(func, arguments, arguments_name, aliases) b.plot() plt.show(b)
def benchmark_2d_ffts(mode='poppy', max_pow=13, verbose=False, savefig=False): """Benchmark FFT runtime vs array size, for multiple transform libraries Parameters ---------- mode : string, 'poppy' or 'base' What to test, either the full poppy usage of the 2D transform, including surrounding setup and normalization code, or the basic transform on its own. max_pow : int Maximum power of 2 array size to test up to verbose : bool output numpy config info savefig : bool save plot result to a PDF? """ # modified based on https://github.com/numpy/numpy/issues/17839#issuecomment-733543850 try: from simple_benchmark import benchmark except ImportError: raise RuntimeError("You must have simple_benchmark installed to run this function") threads = multiprocessing.cpu_count() if mode == 'base': # Functions for benchmarking the low-level FFT functions in each library def pocketfft(it): np.fft.fft2(it) def scipyfft(it): scipy.fft.fft2(it) def fftw_1thread(it): # note, FFTW defaults to 1 thread, unless you override the config, # but that's not a fair comparison pyfftw.interfaces.numpy_fft.fft2(it, thread=1) def fftw(it): # explicitly try multithreaded here pyfftw.interfaces.numpy_fft.fft2(it, threads=threads) def mklfft(it): mkl_fft.fft2(it) funcs_to_test = [pocketfft, fftw, mklfft, scipyfft, fftw_1thread, ] function_aliases = {pocketfft: 'numpy.fft', fftw: 'pyfftw.fft, multithreaded', scipyfft: 'scipy.fft', mklfft: "MKL FFT", fftw_1thread: 'pyfftw.fft, single-thread'} title = "Basic 2D FFT only" elif mode == 'poppy': # Functions for benchmarking poppy transforms using each library def poppy_numpyfft(it): global _USE_FFTW, _USE_MKL _USE_FFTW = False _USE_MKL = False fft_2d(it, fftshift=False) def poppy_fftw(it): global _USE_FFTW, _USE_MKL _USE_FFTW = True _USE_MKL = False fft_2d(it, fftshift=False) def poppy_mklfft(it): global _USE_FFTW, _USE_MKL _USE_FFTW = False _USE_MKL = True fft_2d(it, fftshift=False) funcs_to_test = [poppy_numpyfft, poppy_fftw, poppy_mklfft] function_aliases = {poppy_numpyfft: 'poppy using numpy.fft', poppy_fftw: 'poppy using pyfftw.fft', poppy_mklfft: "poppy using MKL FFT"} title = 'full poppy.accel_math.fft_2d' else: raise ValueError(f"Unknown/invalid value for 'base' parameter: {base}") def shp(len): return (len, len) if verbose: print(np.version) np.show_config() # Turn on the cache for optimum performance pyfftw.interfaces.cache.enable() b_array = benchmark( funcs_to_test, arguments={2 ** i: np.random.uniform(size=shp(2 ** i)) + 1j * np.random.uniform(size=shp(2 ** i)) for i in range(2, max_pow)}, argument_name='array size', function_aliases=function_aliases ) plt.figure(figsize=(12, 8)) b_array.plot() plt.grid(which='both', alpha=0.2) plt.grid(which='major', alpha=0.5) plt.xlim(1, 2e4) plt.ylim(1e-6, 1e1) cpu_label = get_processor_name() + f", {get_physical_cpu_count()} cores" plt.title(f"Time for 2D complex FFTs: {title}\n{cpu_label}", fontweight='bold') if savefig: plt.savefig(f"bench_ffts_{mode}.png")
#!/usr/bin/env python3 # encoding: utf-8 from simple_benchmark import benchmark def ilen_enumerate(xs): len = 0 for len, _ in enumerate(xs, 1): pass return len def ilen_increment(xs): len = 0 for _ in xs: len += 1 return len b = benchmark((ilen_enumerate, ilen_increment), {2**x: [0] * 2**x for x in range(1, 29)}, 'length')
def test_simple(): simple_benchmark.benchmark(funcs=[min, max], arguments=collections.OrderedDict([ (n, [1] * n) for n in [3, 4, 5, 6] ]))