Skip to content

Instantly share code, notes, and snippets.

@tikluganguly
Created October 9, 2017 16:29
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 tikluganguly/36109c9509ace6e14fae05bb8696e207 to your computer and use it in GitHub Desktop.
Save tikluganguly/36109c9509ace6e14fae05bb8696e207 to your computer and use it in GitHub Desktop.
A simple test file to test the performance of vector function execution in any infrastructure
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"This file is going to do a vector multiplication using np.dot and normal for loop based multiplication to test the performance of the underlaying CPU/GPU to execute parallel commands.\n",
"\n",
"Importing required libaries first"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import time\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Creating the vectors"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a = np.random.rand(1000000)\n",
"b = np.random.rand(1000000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First the for loop version of vector multiplication"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"250016.716733\n",
"For Loop 453.6769390106201 ms\n"
]
}
],
"source": [
"t1 = time.time()\n",
"dot = 0\n",
"for i in range(1000000):\n",
" dot += a[i]*b[i]\n",
"t2 = time.time()\n",
"print(dot)\n",
"print(\"For Loop \" + str(1000* (t2-t1)) + \" ms\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now the vectorized version via np.dot"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"250016.716733\n",
"Vectorized version 2.1622180938720703 ms\n"
]
}
],
"source": [
"t1 = time.time()\n",
"dot = np.dot(a,b)\n",
"t2 = time.time()\n",
"print(dot)\n",
"print(\"Vectorized version \" + str(1000* (t2-t1)) + \" ms\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.6",
"language": "python",
"name": "python36"
},
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment