Skip to content

Instantly share code, notes, and snippets.

@nttuan8
Last active April 27, 2019 16:59
Show Gist options
  • Save nttuan8/ffc21f087e4a7d6b9b2c648dc1bd684d to your computer and use it in GitHub Desktop.
Save nttuan8/ffc21f087e4a7d6b9b2c648dc1bd684d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.rand(10000000)\n",
"b = np.random.rand(10000000)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Vectorization time : 0.015958786010742188\n"
]
}
],
"source": [
"start = time.time()\n",
"c = np.dot(a, b.T)\n",
"end = time.time()\n",
"\n",
"print(\"Vectorization time : \", end-start)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Vectorization time : 4.1439173221588135\n"
]
}
],
"source": [
"start = time.time()\n",
"c = 0\n",
"for i in range(len(a)):\n",
" c += a[i] * b[i]\n",
"end = time.time()\n",
"\n",
"print(\"For-loop time : \", end-start)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment