Created
July 24, 2020 21:40
-
-
Save annawoodard/c0b359b661e40cf924a60447b7a9c782 to your computer and use it in GitHub Desktop.
Untitled.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
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import numpy as np", | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def python_matrix_mult(A, B, C):\n for i in range(size):\n for j in range(size):\n for k in range(size):\n C[i][j] += A[i][k] * B[k][j]\n return C", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def numpy_matrix_mult(A, B):\n return np.dot(A, B)", | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "size = 1024\nA = np.random.rand(size, size)\nB = np.random.rand(size, size)\nC = np.zeros((size, size))", | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "python_time = %timeit -o python_matrix_mult(A.tolist(), B.tolist(), C.tolist())", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "1 loop, best of 3: 2min 43s per loop\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "numpy_time = %timeit -o numpy_matrix_mult(A, B)", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "10 loops, best of 3: 22.5 ms per loop\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def timeit_avg(timeit):\n return(sum(timeit.all_runs) / len(timeit.all_runs) / timeit.loops)\n\nprint(\"numpy's absolute speedup: {:.0f}\".format(timeit_avg(python_time) / timeit_avg(numpy_time)))", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "numpy's absolute speedup: 7295\n", | |
"name": "stdout" | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python2", | |
"display_name": "Python 2", | |
"language": "python" | |
}, | |
"language_info": { | |
"mimetype": "text/x-python", | |
"nbconvert_exporter": "python", | |
"name": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.16", | |
"file_extension": ".py", | |
"codemirror_mode": { | |
"version": 2, | |
"name": "ipython" | |
} | |
}, | |
"gist": { | |
"id": "41c31af85190b0c164a7425a51182da8", | |
"data": { | |
"description": "Untitled.ipynb", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/41c31af85190b0c164a7425a51182da8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment