mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-05 20:26:53 +08:00
chore: include nodejs benchmarks (#125)
* chore: include nodejs benchmarks; - Related #124 * chore: fix `factors.js` typo
This commit is contained in:
parent
5927d2cc8f
commit
07321cc660
@ -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:
|
||||
|
@ -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);
|
||||
|
13
tests/benchmark/fib/fib.js
Normal file
13
tests/benchmark/fib/fib.js
Normal 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);
|
19
tests/benchmark/list/list.js
Normal file
19
tests/benchmark/list/list.js
Normal 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);
|
@ -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);
|
||||
|
20
tests/benchmark/primes/primes.js
Normal file
20
tests/benchmark/primes/primes.js
Normal 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);
|
Loading…
Reference in New Issue
Block a user