.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_build/auto_examples/tutorial/01-First_benchmark.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_01-First_benchmark.py: A first benchmark ================= .. GENERATED FROM PYTHON SOURCE LINES 7-15 Let's run and plot a first simple benchmark in Python. Suppose I want to compare the efficiency of a few different methods to pre-allocate memory for a list of strings in Python. Let's define a separate python function for three different list pre-allocation strategies. These functions all take an integer called ``n`` as an input, which stands for the length of the list to be pre-allocated. The argument name ``n`` is the :data:`pyquickbench.default_ax_name` and can be changed in as described in :ref:`sphx_glr__build_auto_examples_tutorial_03-Preparing_inputs.py`. .. GENERATED FROM PYTHON SOURCE LINES 15-34 .. 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 , ] .. GENERATED FROM PYTHON SOURCE LINES 62-63 Let's define relevant sizes of lists to be timed. .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: Python n_bench = 12 all_sizes = [2**n for n in range(n_bench)] .. GENERATED FROM PYTHON SOURCE LINES 68-69 Now, let's import pyquickbench, run and plot the benchmark .. GENERATED FROM PYTHON SOURCE LINES 70-79 .. code-block:: Python import pyquickbench pyquickbench.run_benchmark( all_sizes , all_funs , show = True , ) .. image-sg:: /_build/auto_examples/tutorial/images/sphx_glr_01-First_benchmark_001.png :alt: 01 First benchmark :srcset: /_build/auto_examples/tutorial/images/sphx_glr_01-First_benchmark_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-83 From this benchmark, it is easy to see that initializing a list to a given length filled with a single value is quickest using the star operator. .. _sphx_glr_download__build_auto_examples_tutorial_01-First_benchmark.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 01-First_benchmark.ipynb <01-First_benchmark.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 01-First_benchmark.py <01-First_benchmark.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 01-First_benchmark.zip <01-First_benchmark.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_