.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_build/auto_examples/tutorial/03-Preparing_inputs.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__build_auto_examples_tutorial_03-Preparing_inputs.py: Preparing inputs ================ .. GENERATED FROM PYTHON SOURCE LINES 7-8 By default, the functions to be benchmarked with :func:`pyquickbench.run_benchmark` are expected to take as arguments a single integer ``n`` as in the following code. .. GENERATED FROM PYTHON SOURCE LINES 8-37 .. code-block:: Python def comprehension(n): return ['' for _ in range(n)] def star_operator(n): return ['']*n def for_loop_append(n): l = [] for _ in range(n): l.append('') all_funs = [ comprehension , star_operator , for_loop_append , ] n_bench = 12 all_sizes = [2**n for n in range(n_bench)] pyquickbench.run_benchmark( all_sizes , all_funs , show = True , ) .. image-sg:: /_build/auto_examples/tutorial/images/sphx_glr_03-Preparing_inputs_001.png :alt: 03 Preparing inputs :srcset: /_build/auto_examples/tutorial/images/sphx_glr_03-Preparing_inputs_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-72 Most often however, the functions to be benchmarked take data as input that is more complex than a simple integer and a setup phase is needed. In the following example, we want to compare different implementations of array summation algorithms. .. GENERATED FROM PYTHON SOURCE LINES 72-95 .. code-block:: Python import numpy as np import math def builtin_sum(x): return sum(x) def np_sum(x): return np.sum(x) def m_fsum(x): return math.fsum(x) all_funs = [ builtin_sum , np_sum , m_fsum , ] n_bench = 12 all_sizes = np.array([2**n for n in range(n_bench)]) .. GENERATED FROM PYTHON SOURCE LINES 101-102 Here, we want the benchmark to measure the time taken by the different implementations as a function of the size of the input. The ``setup`` argument expects a callable that provides arguments for our different implementations to be timed. For instance, ``setup`` can return a dictionnnary ``kwargs``, so that each function to be benchmarked will be called as ``fun(**kwargs)``. .. GENERATED FROM PYTHON SOURCE LINES 102-113 .. code-block:: Python def setup(n): x = np.random.random(n) return {'x' : x} pyquickbench.run_benchmark( all_sizes , all_funs , setup = setup , show = True , ) .. image-sg:: /_build/auto_examples/tutorial/images/sphx_glr_03-Preparing_inputs_002.png :alt: 03 Preparing inputs :srcset: /_build/auto_examples/tutorial/images/sphx_glr_03-Preparing_inputs_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 117-118 This calling convention is why positional-only arguments are disallowed in :func:`pyquickbench.run_benchmark`. For instance, even though the following defines a legal Python 3.8+ function .. GENERATED FROM PYTHON SOURCE LINES 118-124 .. code-block:: Python def pos_only_fun(n,/): return n print(pos_only_fun(42)) .. rst-class:: sphx-glr-script-out .. code-block:: none 42 .. GENERATED FROM PYTHON SOURCE LINES 125-126 It is not allowed in :func:`pyquickbench.run_benchmark` since the following raises an error: .. GENERATED FROM PYTHON SOURCE LINES 126-133 .. code-block:: Python try: print(pos_only_fun(n=42)) except TypeError as err: print(f'TypeError: {err}') .. rst-class:: sphx-glr-script-out .. code-block:: none TypeError: pos_only_fun() got some positional-only arguments passed as keyword arguments: 'n' .. GENERATED FROM PYTHON SOURCE LINES 134-135 This comes with hardly any loss of generality since it is possible to wrap these positional-only arguments functions. .. GENERATED FROM PYTHON SOURCE LINES 135-142 .. code-block:: Python def wrap_fun(n): return pos_only_fun(n) print(wrap_fun(42)) print(wrap_fun(n=42)) .. rst-class:: sphx-glr-script-out .. code-block:: none 42 42 .. _sphx_glr_download__build_auto_examples_tutorial_03-Preparing_inputs.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 03-Preparing_inputs.ipynb <03-Preparing_inputs.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 03-Preparing_inputs.py <03-Preparing_inputs.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 03-Preparing_inputs.zip <03-Preparing_inputs.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_