{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "030d70dc-0764-4c3d-a18f-3024ccf94762",
   "metadata": {},
   "source": [
    "# 🐋 循环添加元素的速度问题\n",
    "\n",
    "背景：在数据预处理或特征提取中，常常对多个文件执行特征提取操作，最终将提取的特征合并为一个数组并保存为`.npy`文件。\n",
    "\n",
    "一个小问题是：怎样添加列表能达到最快速度呢？\n",
    "\n",
    "下面简单对比几种元素添加方式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c221578-3c06-440d-85dc-9aca44d4b6bc",
   "metadata": {},
   "source": [
    "## 壹丨对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d7bb9221-0b3d-478d-ab2d-42e2d8e7ef65",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import time\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96e02689-c6d7-403e-9474-9f92907aa4ac",
   "metadata": {},
   "source": [
    "### 1. 使用list的append方法添加，再转numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bcaf5c7c-67d6-4178-acc6-ee7cd97e368a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用时 = 0.10399866104125977\n"
     ]
    }
   ],
   "source": [
    "def run_list():\n",
    "    ss = time.time()\n",
    "    res = []\n",
    "    for i in range(1000000):\n",
    "        res.append(i)\n",
    "    res = np.asarray(res)\n",
    "    print(f'用时 = {time.time() - ss}')\n",
    "\n",
    "run_list()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6906559-ea51-48f0-a0a7-babe0413590c",
   "metadata": {},
   "source": [
    "### 2. 使用list的+=方法添加，再转numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d24d6084-716e-4a2e-9f1b-0b0d8efaf87f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用时 = 0.1379992961883545\n"
     ]
    }
   ],
   "source": [
    "def run_plus():\n",
    "    ss = time.time()\n",
    "    res = []\n",
    "    for i in range(1000000):\n",
    "        res += [i]\n",
    "    res = np.asarray(res)\n",
    "    print(f'用时 = {time.time() - ss}')\n",
    "\n",
    "run_plus()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f69eec1-bf8f-45b7-b752-9bd44853e0b9",
   "metadata": {},
   "source": [
    "### 3. 推导式添加list，再转numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "44599bad-976b-4df8-972b-bc0aef6d0acc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用时 = 0.0859994888305664\n"
     ]
    }
   ],
   "source": [
    "def run_add():\n",
    "    ss = time.time()\n",
    "    res = np.asarray([i for i in range(1000000)])\n",
    "    print(f'用时 = {time.time() - ss}')\n",
    "\n",
    "run_plus()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "340acd3f-e395-4044-91a0-09131ea31c14",
   "metadata": {},
   "source": [
    "### 4. 使用numpy.append方法添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b8bbc095-c3d1-4607-ae33-210316d39355",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用时 = 2.1151256561279297\n"
     ]
    }
   ],
   "source": [
    "def run_np():\n",
    "    ss = time.time()\n",
    "    res = np.array([])\n",
    "    for i in range(100000):\n",
    "        res = np.append(res, i)\n",
    "    print(f'用时 = {time.time() - ss}')\n",
    "\n",
    "run_np()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb13f51f-7e2a-4ccd-9dac-84893ed7862f",
   "metadata": {},
   "source": [
    "## 贰丨总结\n",
    "\n",
    "| 方法             | 时间                |\n",
    "| ---------------- | ------------------- |\n",
    "| list推导式       | 0.0859994888305664  |\n",
    "| list的`append()` | 0.10399866104125977 |\n",
    "| list的`+=`       | 0.1379992961883545  |\n",
    "| `numpy.append()` | 2.1151256561279297  |"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
