Skip to content

Instantly share code, notes, and snippets.

@springcoil
Created January 6, 2016 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save springcoil/7b233953daa4d8907653 to your computer and use it in GitHub Desktop.
Save springcoil/7b233953daa4d8907653 to your computer and use it in GitHub Desktop.
A comparison of generator methods with boring function-based methods for fizz buzz
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# A little comparison of the speeds of various fibonacci functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We often compare Fibonacci implementations. "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"Fizz\n",
"4\n",
"Buzz\n",
"Fizz\n",
"7\n",
"8\n",
"Fizz\n",
"Buzz\n",
"11\n",
"Fizz\n",
"13\n",
"14\n",
"FizzBuzz\n",
"16\n",
"17\n",
"Fizz\n",
"19\n",
"Buzz\n",
"CPU times: user 36 µs, sys: 0 ns, total: 36 µs\n",
"Wall time: 39.1 µs\n"
]
}
],
"source": [
"def fizzbuzz(n):\n",
"\tif n % 3 == 0 and n % 5 == 0:\n",
"\t\treturn 'FizzBuzz'\n",
"\telif n % 5 == 0:\n",
"\t\treturn 'Buzz'\n",
"\telif n % 3 == 0 :\n",
"\t\treturn 'Fizz'\n",
"\telse:\n",
"\t\treturn str(n)\n",
"\n",
"%time print(\"\\n\".join(fizzbuzz(n) for n in range(1, 21)))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def fizzbuzz_boring(n):\n",
"\tfor x in range(1,n):\n",
"\t\tif not x % 15:\n",
"\t\t\tyield 'fizzbuzz'\n",
"\t\telif not x % 5:\n",
"\t\t\tyield 'buzz'\n",
"\t\telif not x % 3:\n",
"\t\t\tyield 'fizz'\n",
"\t\telse:\n",
"\t\t\tyield str(x)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 17 µs, sys: 1 µs, total: 18 µs\n",
"Wall time: 20 µs\n"
]
},
{
"data": {
"text/plain": [
"['1',\n",
" '2',\n",
" 'fizz',\n",
" '4',\n",
" 'buzz',\n",
" 'fizz',\n",
" '7',\n",
" '8',\n",
" 'fizz',\n",
" 'buzz',\n",
" '11',\n",
" 'fizz',\n",
" '13',\n",
" '14',\n",
" 'fizzbuzz',\n",
" '16',\n",
" '17',\n",
" 'fizz',\n",
" '19',\n",
" 'buzz']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%time list(fizzbuzz_boring(21))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see clearly why exactly generator methods save on computational time. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment