Skip to content

Instantly share code, notes, and snippets.

@arun-y
Last active November 3, 2017 03:36
Show Gist options
  • Save arun-y/78fb5e15d07061a72d28cd885c973d32 to your computer and use it in GitHub Desktop.
Save arun-y/78fb5e15d07061a72d28cd885c973d32 to your computer and use it in GitHub Desktop.
A jupyter notebook to show how numpy dot product is faster compare to for loop based dot product. Taken from Andrew Ng deeplearning course
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"250189.622136\n",
"Vectorized Version:2.13003158569\n",
"250189.622136\n",
"For Loop Version:676.230192184\n"
]
}
],
"source": [
"import numpy as np\n",
"import time\n",
"\n",
"a = np.random.rand(1000000)\n",
"b = np.random.rand(1000000)\n",
"\n",
"tic = time.time()\n",
"c = np.dot(a,b)\n",
"toc = time.time()\n",
"\n",
"print (c)\n",
"print (\"Vectorized Version:\" + str(1000*(toc-tic)))\n",
"\n",
"c = 0\n",
"tic = time.time()\n",
"for i in range(1000000):\n",
" c += a[i]*b[i]\n",
"toc = time.time()\n",
"\n",
"print(c)\n",
"print(\"For Loop Version:\" + str(1000*(toc-tic)))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment