Created
March 30, 2013 20:43
-
-
Save rkh/5278252 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
| Rehearsal ----------------------------------------------------------- | |
| normal fib 0.170000 0.000000 0.170000 ( 0.173442) | |
| keyword fib 0.000000 0.000000 0.000000 ( 0.000036) | |
| keyword fib (optimized) 0.000000 0.000000 0.000000 ( 0.000013) | |
| -------------------------------------------------- total: 0.170000sec | |
| user system total real | |
| normal fib 0.180000 0.000000 0.180000 ( 0.179575) | |
| keyword fib 0.000000 0.000000 0.000000 ( 0.000026) | |
| keyword fib (optimized) 0.000000 0.000000 0.000000 ( 0.000008) |
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
| require 'benchmark' | |
| def fib(n) | |
| n < 2 ? n : fib(n-1) + fib(n-2) | |
| end | |
| def fib_keyword(n, result: n.times.inject([0,1]){|(a,b),*|[b,a+b]}.first) | |
| result | |
| end | |
| Benchmark.bmbm do |x| | |
| x.report("normal fib") { fib 30 } | |
| x.report("keyword fib") { fib_keyword 30 } | |
| x.report("keyword fib (optimized)") { fib_keyword 30, result: 832040 } | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment