.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_build/auto_examples/benchmarks/numpy_array_init.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_benchmarks_numpy_array_init.py: Numpy array initialization ========================== .. GENERATED FROM PYTHON SOURCE LINES 7-8 This benchmark compares the execution time of several :class:`numpy:numpy.ndarray` initialization routines. .. GENERATED FROM PYTHON SOURCE LINES 8-45 .. code-block:: Python import numpy as np import pyquickbench dtypes_dict = { "float32" : np.float32, "float64" : np.float64, } def zeros(n, real_dtype): np.zeros((n), dtype=dtypes_dict[real_dtype]) def ones(n, real_dtype): np.ones((n), dtype=dtypes_dict[real_dtype]) def empty(n, real_dtype): np.empty((n), dtype=dtypes_dict[real_dtype]) def full(n, real_dtype): np.full((n), 0., dtype=dtypes_dict[real_dtype]) all_funs = [ zeros , ones , empty , full , ] all_args = { "n" : np.array([2**n for n in range(0, 30)]), "real_dtype" : ["float32", "float64"], } def setup(n, real_dtype): return {'n': n, 'real_dtype':real_dtype} .. GENERATED FROM PYTHON SOURCE LINES 84-109 .. code-block:: Python all_timings = pyquickbench.run_benchmark( all_args , all_funs , setup = setup , filename = filename , StopOnExcept = True , ShowProgress = True , ) plot_intent = { "n" : 'points' , "real_dtype" : 'curve_linestyle' , pyquickbench.fun_ax_name : 'curve_color' , } pyquickbench.plot_benchmark( all_timings , all_args , all_funs , plot_intent = plot_intent , show = True , ) .. image-sg:: /_build/auto_examples/benchmarks/images/sphx_glr_numpy_array_init_001.png :alt: numpy array init :srcset: /_build/auto_examples/benchmarks/images/sphx_glr_numpy_array_init_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 110-116 While these measurement seem surprizing, they are explained in `the numpy documentation `_ : Be mindful that large arrays created with ``np.empty`` or ``np.zeros`` might not be allocated in physical memory until the memory is accessed. [...] One can force pagefaults to occur in the setup phase either by calling ``np.ones`` or ``arr.fill(value)`` after creating the array. .. _sphx_glr_download__build_auto_examples_benchmarks_numpy_array_init.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: numpy_array_init.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: numpy_array_init.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: numpy_array_init.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_