Last active
October 28, 2017 14:09
-
-
Save fkohlgrueber/533fdcbd9b9c5e71774a89462473b2be to your computer and use it in GitHub Desktop.
cython benchmark
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": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%load_ext Cython" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%%cython\n", | |
"\n", | |
"import numpy as np\n", | |
"cimport numpy as cnp\n", | |
"\n", | |
"mu, sigma = 10.64, .35\n", | |
"np.random.seed(1234)\n", | |
"d = np.random.lognormal(mu, sigma, 10000000)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def calculate_tax(d):\n", | |
" tax_seg1 = d[(d > 256303)] * 0.45 - 16164.53\n", | |
" tax_seg2 = d[(d > 54057) & (d <= 256303)] * 0.42 - 8475.44\n", | |
" seg3 = d[(d > 13769) & (d <= 54057)] - 13769\n", | |
" seg4 = d[(d > 8820) & (d <= 13769)] - 8820\n", | |
" prog_seg3 = seg3 * 0.0000022376 + 0.2397\n", | |
" prog_seg4 = seg4 * 0.0000100727 + 0.14\n", | |
" return (\n", | |
" np.sum(tax_seg1) +\n", | |
" np.sum(tax_seg2) +\n", | |
" np.sum(seg3 * prog_seg3 + 939.57) +\n", | |
" np.sum(seg4 * prog_seg4)\n", | |
" ) / np.sum(d)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"%%cython -a\n", | |
"# cython: np_pythran=True\n", | |
"# cython: language=c++\n", | |
"\n", | |
"import numpy as np\n", | |
"cimport numpy as cnp\n", | |
"\n", | |
"def calculate_tax_cython(cnp.ndarray[double, ndim=1] d):\n", | |
" tax_seg1 = d[(d > 256303)] * 0.45 - 16164.53\n", | |
" tax_seg2 = d[(d > 54057) & (d <= 256303)] * 0.42 - 8475.44\n", | |
" seg3 = d[(d > 13769) & (d <= 54057)] - 13769\n", | |
" seg4 = d[(d > 8820) & (d <= 13769)] - 8820\n", | |
" prog_seg3 = seg3 * 0.0000022376 + 0.2397\n", | |
" prog_seg4 = seg4 * 0.0000100727 + 0.14\n", | |
" return (\n", | |
" np.sum(tax_seg1) +\n", | |
" np.sum(tax_seg2) +\n", | |
" np.sum(seg3 * prog_seg3 + 939.57) +\n", | |
" np.sum(seg4 * prog_seg4)\n", | |
" ) / np.sum(d)\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%timeit calculate_tax(d)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%timeit calculate_tax_cython(d)" | |
] | |
}, | |
{ | |
"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.5.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment