chore: include nodejs benchmarks (#125)

* chore: include nodejs benchmarks;

- Related #124

* chore: fix `factors.js` typo
This commit is contained in:
Luke Edwards 2021-06-23 10:47:08 -07:00 committed by GitHub
parent 5927d2cc8f
commit 07321cc660
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 19 deletions

View File

@ -6,11 +6,11 @@ from os.path import join
## All benchmark files. ## TODO: pass args for iterations.
benchmarks = {
"factors" : ['.pk', '.py', '.rb', '.wren'],
"fib" : ['.pk', '.py', '.rb', '.wren'],
"list" : ['.pk', '.py', '.rb'],
"loop" : ['.pk', '.py', '.rb', ".wren"],
"primes" : ['.pk', '.py', '.rb', ".wren"],
"factors" : ['.pk', '.py', '.rb', '.js', '.wren'],
"fib" : ['.pk', '.py', '.rb', '.js', '.wren'],
"list" : ['.pk', '.py', '.rb', '.js'],
"loop" : ['.pk', '.py', '.rb', '.js', '.wren'],
"primes" : ['.pk', '.py', '.rb', '.js', '.wren'],
}
def main():
@ -24,6 +24,7 @@ def run_all_benchmarks():
if file.endswith('.py' ) : return 'python'
if file.endswith('.rb' ) : return 'ruby'
if file.endswith('.wren') : return 'wren'
if file.endswith('.js' ) : return 'node'
assert False
for bm_name in benchmarks:

View File

@ -3,13 +3,14 @@
// which makes more faster than other bytecode interpreted
// VM language listed here.
var start = +new Date();
var start = process.hrtime();
var N = 50000000;
var factors = []
for (var i = 0; i <= N; i++) {
if (N % i == 0) factors.push(i);
var i=0, factors = []
for (; i <= N; i++) {
if (N % i === 0) factors.push(i);
}
var end = +new Date();
console.log('elapsed: ' + (end - start)/1000 + 's');
var end = process.hrtime(start);
var secs = (end[0] + end[1] / 1e9).toFixed(6) + 's';
console.log('elapsed:', secs);

View File

@ -0,0 +1,13 @@
function fib(n) {
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
}
var start = process.hrtime();
for (var i=0; i < 10; i++) {
process.stdout.write(fib(30) + '\n');
}
var end = process.hrtime(start);
var secs = (end[0] + end[1] / 1e9).toFixed(6) + 's';
console.log('elapsed:', secs);

View File

@ -0,0 +1,19 @@
function reverse(list) {
var i=0, tmp, idx, count=list.length;
for (; i < count; i++) {
idx = count - i - 1;
tmp = list[idx];
list[idx] = list[i];
list[i] = tmp;
}
}
var start = process.hrtime();
var i=0, N=20000000, list=Array(N);
for (; i < N; i++) list[i] = i;
reverse(list);
var end = process.hrtime(start);
var secs = (end[0] + end[1] / 1e9).toFixed(6) + 's';
console.log('elapsed:', secs);

View File

@ -3,13 +3,15 @@
// which makes more faster than other bytecode interpreted
// VM language listed here.
var start = +new Date();
var start = process.hrtime();
list = []
for (var i = 0; i < 10000000; i++) { list.push(i) }
sum = 0
for (var i = 0; i < list.length; i++) { sum += list[i]; }
console.log(sum)
var i=0, list=[];
for (; i < 10000000; i++) list.push(i);
var end = +new Date();
console.log('elapsed: ' + (end - start)/1000 + 's');
var sum = 0;
for (i=0; i < list.length; i++) sum += list[i];
console.log(sum);
var end = process.hrtime(start);
var secs = (end[0] + end[1] / 1e9).toFixed(6) + 's';
console.log('elapsed:', secs);

View File

@ -0,0 +1,20 @@
function isPrime(n) {
if (n < 2) return false;
for (var i=2; i < n; i++) {
if (n % i === 0) return false;
}
return true;
}
var start = process.hrtime();
var i=0, N=30000, primes=[];
for (; i < N; i++) {
if (isPrime(i)) {
primes.push(i);
}
}
var end = process.hrtime(start);
var secs = (end[0] + end[1] / 1e9).toFixed(6) + 's';
console.log('elapsed:', secs);