Created
October 26, 2013 21:18
-
-
Save john9631/7174654 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"metadata": { | |
"language": "Julia", | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"function fmemoize(f)\n", | |
"\tmem(x) = haskey(cache, x) ? cache[x] : (cache[x] = f(x))\n", | |
"\tcache = Dict()\n", | |
"\treturn mem\n", | |
"end\n", | |
"\n", | |
"function memofib(n)\n", | |
" fib(n=1) = n <= 1 ? 1 : fib(n-1) + fib(n-2)\n", | |
" fib = fmemoize(fib)\n", | |
" @time fib(n)\n", | |
" return fib(n)\n", | |
"end\n", | |
"\n", | |
"fb(n=1) = n <= 1 ? 1 : fb(n-1) + fb(n-2)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 3, | |
"text": [ | |
"fb (generic function with 2 methods)" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print(\"Standard fib for 40: \")\n", | |
"@time fb(40)\n", | |
"print(\"Memoized fib for 1500: \")\n", | |
"memofib(1500)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Standard fib for 40: elapsed time: " | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0.920763416 seconds (25728 bytes allocated)\n", | |
"Memoized fib for 1500: elapsed time: 0.037157493 seconds (2334536 bytes allocated)\n" | |
] | |
}, | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": [ | |
"1415338001064792265" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"using Memoize\n", | |
"@memoize function fib(n=1) \n", | |
" n <= 1 ? 1 : fib(n-1) + fib(n-2)\n", | |
"end\n", | |
"\n", | |
"@time fib(1500)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"elapsed time: 0." | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"007411431 seconds (540764 bytes allocated)\n" | |
] | |
}, | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": [ | |
"1415338001064792265" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment