-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfibs.js
More file actions
38 lines (31 loc) · 787 Bytes
/
fibs.js
File metadata and controls
38 lines (31 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const ffi = require('ffi')
const isWin = /^win/.test(process.platform)
const rust = ffi.Library('target/release/' + (!isWin ? 'lib' : '') + 'ffi', {
fib: ['int', ['int']]
})
const c = ffi.Library('target/release/build/libfib', {
'fib': [ 'uint64', [ 'int' ] ]
})
const args = process.argv
if (args.length < 3) {
console.log('Arguments: ' + args[0] + ' ' + args[1] + ' ???')
process.exit()
} else if (+args[2] !== +args[2]) {
console.log('Argument is not a number!')
process.exit()
}
const input = parseInt(args[2])
function fib(n) {
if (n < 3) {
return 1
}
return fib(n - 1) + fib(n - 2)
}
function benchmark(str, fn) {
console.time(str)
console.log(fn(input))
console.timeEnd(str)
}
benchmark('node', fib)
benchmark('rust', rust.fib)
benchmark('c', c.fib)