{ "cells": [ { "cell_type": "markdown", "id": "3091c3d2", "metadata": {}, "source": [ "# Array-oriented programming – vectorisation\n", "\n", "Using NumPy arrays allows you to express many types of data processing tasks as concise array expressions that would otherwise require writing `for`-loops. This practice of replacing loops with array expressions is also called _vectorisation_. In general, vectorised array operations are significantly faster than their pure Python equivalents." ] }, { "cell_type": "code", "execution_count": 1, "id": "bca0db79", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "id": "d86c6a95", "metadata": {}, "source": [ "First we create a NumPy array with one hundred thousand integers:" ] }, { "cell_type": "code", "execution_count": 2, "id": "115bba7a", "metadata": {}, "outputs": [], "source": [ "myarray = np.arange(100000)" ] }, { "cell_type": "markdown", "id": "ef42524a", "metadata": {}, "source": [ "Then we square all the elements in this array with [numpy.square](https://numpy.org/doc/stable/reference/generated/numpy.square.html):" ] }, { "cell_type": "code", "execution_count": 3, "id": "eee5c824", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 185 μs, sys: 994 μs, total: 1.18 ms\n", "Wall time: 107 μs\n" ] }, { "data": { "text/plain": [ "array([ 0, 1, 4, ..., 9999400009, 9999600004,\n", " 9999800001])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time np.square(myarray)" ] }, { "cell_type": "markdown", "id": "3e20887d", "metadata": {}, "source": [ "For comparison, we now measure the time of Python’s quadratic function:" ] }, { "cell_type": "code", "execution_count": 4, "id": "0259e4c5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 2.17 ms, sys: 7.11 ms, total: 9.28 ms\n", "Wall time: 1.22 ms\n" ] } ], "source": [ "%time for _ in range(10): myarray2 = myarray ** 2" ] }, { "cell_type": "markdown", "id": "4f57a382", "metadata": {}, "source": [ "And finally, we compare the time with the calculation of the quadratic function of all values of a Python list:" ] }, { "cell_type": "code", "execution_count": 5, "id": "dc1696f8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 116 ms, sys: 365 ms, total: 480 ms\n", "Wall time: 47.5 ms\n" ] } ], "source": [ "mylist = list(range(100000))\n", "%time for _ in range(10): mylist2 = [x ** 2 for x in mylist]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }