Last active
October 23, 2017 17:24
-
-
Save josmas/278f3586a76d94380cdd2ba59ec5d883 to your computer and use it in GitHub Desktop.
Vectorisation Examples GDGDublin DevFest17
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": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Vectorisation Examples for GDGDublin DevFest 17\n", | |
"Using the %timeit function to track and print processing times" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"14.6 ns ± 0.139 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"# %timeit examples\n", | |
"%timeit 1 + 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"14.7 ns ± 0.113 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"# accessing the results of %timeit\n", | |
"result = %timeit -o 1 + 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.53 µs ± 43.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n", | |
"465 ns ± 6.67 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"# Create three arrays in Python\n", | |
"prices_arr = [9, 12, 14, 8, 6, 11]\n", | |
"quantities_arr = [1, 1, 1, 1, 1, 1]\n", | |
"total_arr = []\n", | |
"\n", | |
"# Create three Numpy arrays from the previous ones\n", | |
"prices = np.array(prices_arr)\n", | |
"quantities = np.array(quantities_arr)\n", | |
"total = np.array(total_arr)\n", | |
"\n", | |
"# Simply multiply prices and quantities\n", | |
"def p_times_q_in_loop():\n", | |
" for index, x in enumerate(prices_arr):\n", | |
" total_arr.append(x * quantities_arr[index])\n", | |
"\n", | |
"# Track time for function and using a vectorised version of the function\n", | |
"using_loop = %timeit -o p_times_q_in_loop()\n", | |
"using_vectorisaiton = %timeit -o total = prices * quantities" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2.92 µs ± 57.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n", | |
"465 ns ± 6.67 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"def p_times_q_in_loop(prices, quantities):\n", | |
" for index, x in enumerate(prices):\n", | |
" total_arr.append(x * quantities[index])\n", | |
"\n", | |
"using_loop_and_parameters = %timeit -o p_times_q_in_loop(prices, quantities)\n", | |
"print(using_vectorisaiton)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"16.2 ms ± 374 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n", | |
"37.1 µs ± 1.08 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"# large arrays\n", | |
"large_prices = np.random.random_sample(50000)\n", | |
"large_quantities = np.random.random_sample(50000)\n", | |
"\n", | |
"large_using_loop_and_parameters = %timeit -o p_times_q_in_loop(large_prices, large_quantities)\n", | |
"large_using_vectorisaiton = %timeit -o total = large_prices * large_quantities" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"17.4 s ± 568 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", | |
"228 ms ± 13.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" | |
] | |
} | |
], | |
"source": [ | |
"# large_R_ arrays\n", | |
"large_prices = np.random.random_sample(50000000)\n", | |
"large_quantities = np.random.random_sample(50000000)\n", | |
"\n", | |
"large_using_loop_and_parameters = %timeit -o p_times_q_in_loop(large_prices, large_quantities)\n", | |
"large_using_vectorisaiton = %timeit -o total = large_prices * large_quantities\n" | |
] | |
} | |
], | |
"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