Created
November 9, 2015 05:00
-
-
Save zakki/a5a93ceec008446032eb to your computer and use it in GitHub Desktop.
fib
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
$ time node fib.asm.js | |
102334155 | |
node fib.asm.js 0.00s user 0.01s system 0% cpu 1.723 total | |
$ time node fib.js | |
102334155 | |
node fib.js 0.00s user 0.01s system 0% cpu 2.025 total | |
$ cat fib.asm.js | |
function Module() { | |
'use asm'; | |
function fib(n) { | |
n = n | 0; | |
var a = 0, b = 0, r = 0; | |
if ((n | 0) <= 1) { | |
return n | 0; | |
} | |
a = n - 1 | 0; | |
b = n - 2 | 0; | |
r = (fib(a) | 0) + (fib(b) | 0) | 0; | |
return r | 0; | |
} | |
function main() { | |
return fib(40) | 0; | |
} | |
return { | |
fib: fib, | |
main: main | |
}; | |
} | |
var module = Module(); | |
console.log(module.main()); | |
$ cat fib.js | |
function fib(n){ | |
if(n <= 1) { | |
return n; | |
} | |
return fib(n - 1) + fib(n - 2); | |
} | |
console.log(fib(40)); | |
$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment