Skip to content

Instantly share code, notes, and snippets.

@lukassup
Created May 28, 2017 16:32
Show Gist options
  • Select an option

  • Save lukassup/5cd097d761b838bb56dec884d1264eb1 to your computer and use it in GitHub Desktop.

Select an option

Save lukassup/5cd097d761b838bb56dec884d1264eb1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CPython"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def fib1(n):\n",
" \"\"\"Get the n-th Fibonacci sequence number.\n",
" \n",
" Iterative implementation in CPython, variant 1.\n",
" \"\"\"\n",
" a, b = 0, 1\n",
" for _ in range(n):\n",
" a, b = b, a + b\n",
" return a\n",
"\n",
"\n",
"def fib2(n):\n",
" \"\"\"Get the n-th Fibonacci sequence number.\n",
" \n",
" Iterative implementation in CPython, variant 2.\n",
" \"\"\"\n",
" a, b = 0, 1\n",
" while n > 0:\n",
" a, b = b, a + b\n",
" n -= 1\n",
" return a"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.28 µs ± 44.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"fib1(100)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9 µs ± 250 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"fib2(100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Cython\n",
"\n",
"Extra performance for free\n",
"\n",
"```bash\n",
"$ pip install Cython\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"%load_ext Cython"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%%cython\n",
"def fib3(int n):\n",
" \"\"\"Get the n-th Fibonacci sequence number.\n",
" \n",
" Iterative implementation in Cython, variant 1.\n",
" \"\"\"\n",
" if n <= 2:\n",
" return n\n",
" cdef unsigned long long a = 0\n",
" cdef unsigned long long b = 1\n",
" for _ in range(n):\n",
" a, b = b, a + b\n",
" return a\n",
"\n",
"\n",
"def fib4(int n):\n",
" \"\"\"Get the n-th Fibonacci sequence number.\n",
" \n",
" Iterative implementation in Cython, variant 2.\n",
" \"\"\"\n",
" if n <= 2:\n",
" return n\n",
" cdef unsigned long long a = 0\n",
" cdef unsigned long long b = 1\n",
" while n > 0:\n",
" a, b = b, a + b\n",
" n -= 1\n",
" return a"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"151 ns ± 1.33 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"fib3(100)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"143 ns ± 1.76 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"fib4(100)"
]
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment