{ "cells": [ { "cell_type": "markdown", "id": "7eef3dcc", "metadata": {}, "source": [ "# Introduction to NumPy" ] }, { "cell_type": "markdown", "id": "a2256613", "metadata": {}, "source": [ "NumPy operations perform complex calculations on entire arrays without the need for Python `for` loops, which can be slow for large sequences. NumPy’s speed is explained by its C-based algorithms, which avoid the overhead of Python code. To give you an idea of the performance difference, we measure the difference between a NumPy array and a Python list with a hundred thousand integers:" ] }, { "cell_type": "code", "execution_count": 1, "id": "b0815cd8", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "8764f708", "metadata": {}, "outputs": [], "source": [ "myarray = np.arange(100000)\n", "mylist = list(range(100000))" ] }, { "cell_type": "code", "execution_count": 3, "id": "10dfdb7a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 15 ms, sys: 76.9 ms, total: 91.9 ms\n", "Wall time: 10.4 ms\n" ] } ], "source": [ "%time for _ in range(10): myarray2 = myarray ** 2" ] }, { "cell_type": "code", "execution_count": 4, "id": "38963e40", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 67 ms, sys: 181 ms, total: 248 ms\n", "Wall time: 40.3 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 }