From 07321cc6603ca811f4e8ac2d52c3c2662e444104 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Wed, 23 Jun 2021 10:47:08 -0700 Subject: [PATCH] chore: include nodejs benchmarks (#125) * chore: include nodejs benchmarks; - Related #124 * chore: fix `factors.js` typo --- tests/benchmark/benchmarks.py | 11 ++++++----- tests/benchmark/factors/factors.js | 13 +++++++------ tests/benchmark/fib/fib.js | 13 +++++++++++++ tests/benchmark/list/list.js | 19 +++++++++++++++++++ tests/benchmark/loop/loop.js | 18 ++++++++++-------- tests/benchmark/primes/primes.js | 20 ++++++++++++++++++++ 6 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 tests/benchmark/fib/fib.js create mode 100644 tests/benchmark/list/list.js create mode 100644 tests/benchmark/primes/primes.js diff --git a/tests/benchmark/benchmarks.py b/tests/benchmark/benchmarks.py index 76a1c34..861d184 100644 --- a/tests/benchmark/benchmarks.py +++ b/tests/benchmark/benchmarks.py @@ -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: diff --git a/tests/benchmark/factors/factors.js b/tests/benchmark/factors/factors.js index d8a633b..26b6ef3 100644 --- a/tests/benchmark/factors/factors.js +++ b/tests/benchmark/factors/factors.js @@ -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); diff --git a/tests/benchmark/fib/fib.js b/tests/benchmark/fib/fib.js new file mode 100644 index 0000000..574efcb --- /dev/null +++ b/tests/benchmark/fib/fib.js @@ -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); diff --git a/tests/benchmark/list/list.js b/tests/benchmark/list/list.js new file mode 100644 index 0000000..b622bae --- /dev/null +++ b/tests/benchmark/list/list.js @@ -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); diff --git a/tests/benchmark/loop/loop.js b/tests/benchmark/loop/loop.js index 3ed1f90..cfd4fbd 100644 --- a/tests/benchmark/loop/loop.js +++ b/tests/benchmark/loop/loop.js @@ -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); diff --git a/tests/benchmark/primes/primes.js b/tests/benchmark/primes/primes.js new file mode 100644 index 0000000..bc47805 --- /dev/null +++ b/tests/benchmark/primes/primes.js @@ -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);