Skip to content

Instantly share code, notes, and snippets.

@cgranade
Last active May 12, 2016 05:25
Show Gist options
  • Save cgranade/c0e1290327441b96aa888bda13f4aeaa to your computer and use it in GitHub Desktop.
Save cgranade/c0e1290327441b96aa888bda13f4aeaa to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from __future__ import division"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import numba as nb"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"A = np.random.random((60000000,))\n",
"B = np.random.random((60000000,))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loops, best of 3: 2.3 s per loop\n"
]
}
],
"source": [
"%timeit np.cos(A) * np.cos(B)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loops, best of 3: 2.99 s per loop\n"
]
}
],
"source": [
"%timeit (np.cos(A + B) + np.cos(A - B)) / 2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"@nb.jit(nopython=True)\n",
"def unreduced_expr(A, B):\n",
" return np.cos(A) * np.cos(B)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loops, best of 3: 1.76 s per loop\n"
]
}
],
"source": [
"%timeit unreduced_expr(A, B)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"@nb.jit(nopython=True)\n",
"def reduced_expr(A, B):\n",
" return (np.cos(A + B) + np.cos(A - B)) / 2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loops, best of 3: 1.93 s per loop\n"
]
}
],
"source": [
"%timeit reduced_expr(A, B)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment