Skip to content

Instantly share code, notes, and snippets.

@seibert
Created February 15, 2015 15:15
Show Gist options
  • Save seibert/49712e043ae7e0b59cba to your computer and use it in GitHub Desktop.
Save seibert/49712e043ae7e0b59cba to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:dc12187573dcd80436da771cd37a58ac934ed9f0ea648dbecfc580ec68db051d"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import numba"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@numba.vectorize(['float64(float64, float64)'])\n",
"def vectorize_func(a, b):\n",
" return a ** 2 + b\n",
"\n",
"@numba.jit\n",
"def jit_func(a, b):\n",
" res = np.empty_like(a)\n",
" for i in range(len(a)):\n",
" res[i] = a[i] ** 2 + b[i]\n",
" return res"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = np.arange(1000, dtype=np.float64)\n",
"y = x + 1"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"np.testing.assert_array_almost_equal(x ** 2 + y, vectorize_func(x, y))\n",
"np.testing.assert_array_almost_equal(x ** 2 + y, jit_func(x, y))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit x ** 2 + y\n",
"%timeit vectorize_func(x, y)\n",
"%timeit jit_func(x, y)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100000 loops, best of 3: 3.28 \u00b5s per loop\n",
"1000000 loops, best of 3: 1.26 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1000000 loops, best of 3: 1.25 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment