{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "#"
      ],
      "id": "09f9d614-0e5e-4238-8cda-422ea4d2b446"
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np"
      ],
      "id": "201e36f1-c755-4185-8e5d-15f29872828f"
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [],
      "source": [
        "def f(size):\n",
        "    a = np.zeros(size) # pre-allocate arrays\n",
        "    for i in range(size):\n",
        "        a[i] = 2 ** i\n",
        "    return a"
      ],
      "id": "b8592a78-6e8e-45e9-8b0f-b871722de52c"
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [],
      "source": [
        "def g(size):\n",
        "    return 2.0 ** np.arange(size)"
      ],
      "id": "1d0f809a-424f-4e58-b686-2e9e4ba2a81f"
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "284 µs ± 4.03 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)"
          ]
        }
      ],
      "source": [
        "%timeit f(1000)"
      ],
      "id": "f6d76a65-29bf-470b-b907-952bccb85679"
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "3.49 µs ± 26.8 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)"
          ]
        }
      ],
      "source": [
        "%timeit g(1000)"
      ],
      "id": "75b0d61f-acd4-4933-a02c-da8def312cc2"
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {},
      "outputs": [],
      "source": [
        "def h(size):\n",
        "    l = []\n",
        "    for i in range(size):\n",
        "        l.append(2 ** i)\n",
        "    return l"
      ],
      "id": "1372ef37-275b-4222-91fd-dc1e65ee8532"
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "229 µs ± 2.17 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)"
          ]
        }
      ],
      "source": [
        "%timeit h(1000)"
      ],
      "id": "4469839a-d67d-4a15-b789-c4e121ff3049"
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {},
      "outputs": [],
      "source": [
        "def h(size):\n",
        "    l = [0] * size\n",
        "    for i in range(size):\n",
        "        l[i] = 2 ** i\n",
        "    return l"
      ],
      "id": "5bfd4b70-8a0e-4143-b2b9-5bd82485f535"
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "232 µs ± 3.43 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)"
          ]
        }
      ],
      "source": [
        "%timeit h(1000)"
      ],
      "id": "ea44c577-75a4-40cc-9512-a712cba74a9f"
    }
  ],
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3 (ipykernel)",
      "language": "python"
    },
    "language_info": {
      "name": "python",
      "codemirror_mode": {
        "name": "ipython",
        "version": "3"
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  }
}