{ "cells": [ { "cell_type": "markdown", "id": "f020bc3e", "metadata": {}, "source": [ "# Array-orientierte Programmierung – Vektorisierung\n", "\n", "Die Verwendung von NumPy-Arrays ermöglicht euch, viele Arten von Datenverarbeitungsaufgaben als prägnante Array-Ausdrücke auszudrücken, die andernfalls das Schreiben von `for`-Schleifen erfordern würden. Diese Praxis, Schleifen durch Array-Ausdrücke zu ersetzen, wird auch _Vektorisierung_ genannt. Im Allgemeinen sind vektorisierte Array-Operationen deutlich schneller als ihre reinen Python-Entsprechungen." ] }, { "cell_type": "code", "execution_count": 1, "id": "96d50f7c", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "id": "fc2b8ae2", "metadata": {}, "source": [ "Zunächst erstellen wir ein NumPy-Array mit hunderttausend Ganzzahlen:" ] }, { "cell_type": "code", "execution_count": 2, "id": "3189d3db", "metadata": {}, "outputs": [], "source": [ "myarray = np.arange(100000)" ] }, { "cell_type": "markdown", "id": "844c0908", "metadata": {}, "source": [ "Anschließend quadrieren wir alle Elemente in diesem Array mit [numpy.square](https://numpy.org/doc/stable/reference/generated/numpy.square.html):" ] }, { "cell_type": "code", "execution_count": 3, "id": "942834ca", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 155 μs, sys: 119 μs, total: 274 μs\n", "Wall time: 146 μ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": "67214d2b", "metadata": {}, "source": [ "Zum Vergleich messen wir nun die Zeit der quadratischen Funktion von Python:" ] }, { "cell_type": "code", "execution_count": 4, "id": "b521de6b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 958 μs, sys: 1.09 ms, total: 2.04 ms\n", "Wall time: 1.14 ms\n" ] } ], "source": [ "%time for _ in range(10): myarray2 = myarray**2" ] }, { "cell_type": "markdown", "id": "6215ccb8", "metadata": {}, "source": [ "Und schließlich vergleichen wir die Zeit noch mit der Berechnung der quadratischen Funktion aller Werte einer Python-Liste:" ] }, { "cell_type": "code", "execution_count": 5, "id": "45b171be", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 26 ms, sys: 2.81 ms, total: 28.8 ms\n", "Wall time: 28.4 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 }