Created
December 18, 2018 11:45
-
-
Save simecek/5280e202df1a2b39e412a7f438b2acc1 to your computer and use it in GitHub Desktop.
Numpy append is slow.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Numpy append is slow.ipynb", | |
"version": "0.3.2", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/simecek/5280e202df1a2b39e412a7f438b2acc1/numpy-append-is-slow.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "oymWg-2UDrnk", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import numpy as np\n", | |
"from collections import deque" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "N3q07H-FGepZ", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## List" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "LKG_SBvJEO_d", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 52 | |
}, | |
"outputId": "6d46c457-d79c-43b2-d092-a4ac1de0a881" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"%%time\n", | |
"\n", | |
"l = list()\n", | |
"for i in range(10000):\n", | |
"\tl.append(i)" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 1.96 ms, sys: 0 ns, total: 1.96 ms\n", | |
"Wall time: 1.97 ms\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "7qzfupc8Ghlh", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Deque" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "LQyOQgwHD2Pg", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 52 | |
}, | |
"outputId": "981caacf-da4d-434d-b23d-2ff0851e971f" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"%%time\n", | |
"\n", | |
"d = deque()\n", | |
"for i in range(10000):\n", | |
"\td.append(i)" | |
], | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 1.76 ms, sys: 0 ns, total: 1.76 ms\n", | |
"Wall time: 1.77 ms\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "xyMww8GyGlFl", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Numpy (append)" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "l_EM2rEgEdmU", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 52 | |
}, | |
"outputId": "f3816ed3-273a-4b46-e372-946cfa7a84f4" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"%%time\n", | |
"\n", | |
"a = np.array([])\n", | |
"for i in range(10000):\n", | |
"\ta = np.append(a, [i])" | |
], | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 70.7 ms, sys: 947 µs, total: 71.7 ms\n", | |
"Wall time: 74 ms\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "qppFnY-QG3-5", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Numpy (allocate the array beforehand)" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "yvZ23jlyG0uO", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 52 | |
}, | |
"outputId": "03bf9d17-0f4e-406c-e255-a724b994eb12" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"%%time\n", | |
"\n", | |
"a = np.zeros(10000)\n", | |
"for i in range(10000):\n", | |
"\ta[i] = i" | |
], | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 1.62 ms, sys: 0 ns, total: 1.62 ms\n", | |
"Wall time: 1.58 ms\n" | |
], | |
"name": "stdout" | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment