Skip to content

Instantly share code, notes, and snippets.

@vrishaligiri
Created November 25, 2017 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrishaligiri/34539633fffaab9df52aa35c88078880 to your computer and use it in GitHub Desktop.
Save vrishaligiri/34539633fffaab9df52aa35c88078880 to your computer and use it in GitHub Desktop.
Code Snippets
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4]\n"
]
}
],
"source": [
"import numpy as np\n",
"a = np.array([1,2,3,4])\n",
"print (a)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2499522.8054\n",
"Time with Vectorization :28.001785278320312ms\n",
"2499522.8054\n",
"For loop :5648.353099822998ms\n"
]
}
],
"source": [
"import time\n",
"\n",
"a = np.random.rand(10000000)\n",
"b = np.random.rand(10000000)\n",
"\n",
"tic = time.time()\n",
"c = np.dot(a,b)\n",
"toc = time.time()\n",
"print(c)\n",
"print(\"Time with Vectorization :\" + str(1000*(toc-tic)) +\"ms\")\n",
"\n",
"c = 0\n",
"tic = time.time()\n",
"for i in range(10000000):\n",
" c += a[i]*b[i]\n",
"toc = time.time()\n",
"print(c)\n",
"print(\"For loop :\" + str(1000*(toc-tic)) +\"ms\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment