.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/benchmarks/sweep_runtime.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_benchmarks_sweep_runtime.py: Runtime scaling with sweep size ================================ Measure how long a ``Qsca`` calculation takes as the number of sphere diameters increases. .. GENERATED FROM PYTHON SOURCE LINES 8-54 .. image-sg:: /gallery/benchmarks/images/sphx_glr_sweep_runtime_001.png :alt: PyMieSim runtime scaling :srcset: /gallery/benchmarks/images/sphx_glr_sweep_runtime_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 10 diameters | 0.0005 s | 10 results 25 diameters | 0.0006 s | 25 results 50 diameters | 0.0009 s | 50 results 100 diameters | 0.0015 s | 100 results 200 diameters | 0.0026 s | 200 results 400 diameters | 0.0050 s | 400 results | .. code-block:: Python import time import matplotlib.pyplot as plt import numpy as np from PyMieSim import Experiment, GaussianSet, PolarizationSet, SphereSet, ureg def run_sweep(number_of_diameters: int) -> tuple[float, int]: source = GaussianSet( wavelength=[600] * ureg.nanometer, polarization=PolarizationSet(angles=[0] * ureg.degree), optical_power=[1e-3] * ureg.watt, numerical_aperture=[0.2], ) scatterer = SphereSet( diameter=np.linspace(100, 1000, number_of_diameters) * ureg.nanometer, material=[1.5], medium=[1.0], ) experiment = Experiment(scatterer_set=scatterer, source_set=source) start = time.perf_counter() values = experiment.get("Qsca").as_numpy() elapsed = time.perf_counter() - start return elapsed, int(np.asarray(values).size) sweep_sizes = [10, 25, 50, 100, 200, 400] measurements = [run_sweep(size) for size in sweep_sizes] runtime_seconds, result_sizes = np.asarray(measurements).T for size, runtime, result_size in zip(sweep_sizes, runtime_seconds, result_sizes): print(f"{size:4d} diameters | {runtime:.4f} s | {int(result_size)} results") figure, axis = plt.subplots() axis.plot(sweep_sizes, runtime_seconds, marker="o") axis.set( xlabel="Number of diameters", ylabel="Runtime [s]", title="PyMieSim runtime scaling", ) axis.grid(True, alpha=0.3) figure.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.228 seconds) .. _sphx_glr_download_gallery_benchmarks_sweep_runtime.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: sweep_runtime.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: sweep_runtime.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: sweep_runtime.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_