{ "cells": [ { "cell_type": "markdown", "id": "38091f92", "metadata": {}, "source": [ "# Einführung in NumPy" ] }, { "cell_type": "markdown", "id": "e0f64e9a", "metadata": {}, "source": [ "NumPy-Operationen führen komplexe Berechnungen auf ganzen Arrays durch, ohne dass Python `for`-Schleifen erforderlich sind, die bei großen Sequenzen langsam sein können. Die Schnelligkeit von NumPy erklärt sich aus den C-basierten Algorithmen, die den Overhead des Python-Codes vermeidet. Um euch einen Eindruck zu vermitteln vom Performance-Unterschied, messen wir den Unterschied zwischen einem NumPy-Array un einer Python-Liste mit hunderttausend Ganzzahlen:" ] }, { "cell_type": "code", "execution_count": 1, "id": "58a6f298", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "4a1c7b5f", "metadata": {}, "outputs": [], "source": [ "myarray = np.arange(100000)\n", "mylist = list(range(100000))" ] }, { "cell_type": "code", "execution_count": 3, "id": "fd429e60", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 829 μs, sys: 1.04 ms, total: 1.87 ms\n", "Wall time: 1.01 ms\n" ] } ], "source": [ "%time for _ in range(10): myarray2 = myarray ** 2" ] }, { "cell_type": "code", "execution_count": 4, "id": "2747e294", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 23.3 ms, sys: 1.99 ms, total: 25.3 ms\n", "Wall time: 25.1 ms\n" ] } ], "source": [ "%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 }