Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add memoize to the build. Fix it so it builds, and at least part of i…
…t runs. Add in a benchmark which shows the performance difference between a naive recursive fibonacci, and a version using a memoized Y combinator
- Loading branch information
1 parent
ca4183b
commit 92ae143
Showing
4 changed files
with
62 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function main[main]() | ||
{ | ||
count_time("fib(30) unmemoized", function() { | ||
say(sprintf("fib(30) = %f", [fib_standard(30)])); | ||
}); | ||
|
||
count_time("fib(30) with memoization", function() { | ||
load_bytecode("rosella/memoize.pbc"); | ||
using Rosella.Memoize.Y; | ||
var answer = (Y(function(g) { | ||
var _g = g; | ||
return function(int n) { | ||
if (n <= 1) | ||
return n; | ||
return _g(n - 1) + _g(n - 2); | ||
}; | ||
}))(30); | ||
say(sprintf("fib(30) = %f", [answer])); | ||
}); | ||
} | ||
|
||
function count_time(string description, var code) | ||
{ | ||
say(sprintf("Starting %s", [description])); | ||
float starttime = floattime(); | ||
code(); | ||
float endtime = floattime(); | ||
say(sprintf("Total time: %fs", [endtime - starttime])); | ||
} | ||
|
||
function fib_standard(int i) | ||
{ | ||
if (i <= 1) | ||
return i; | ||
return fib_standard(i - 1) + fib_standard(i - 2); | ||
} |
This file contains 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
This file contains 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
This file contains 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