Skip to content

Instantly share code, notes, and snippets.

@peterfoxflick
Last active August 24, 2019 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterfoxflick/ab6db2fa436eb5755c748cbb4da8c813 to your computer and use it in GitHub Desktop.
Save peterfoxflick/ab6db2fa436eb5755c748cbb4da8c813 to your computer and use it in GitHub Desktop.
require 'benchmark'
%x(rm output.csv)
file = File.open('output.csv', 'a')
file.puts("N,DoWhile,ForEach,ForOf,ForLoop,While")
file.close
10000.times{ |i|
file = File.open('output.csv', 'a')
time = Benchmark.measure {
%x(node dowhile.js)
}
output = (i + 1).to_s + "," + time.real.to_s
time = Benchmark.measure {
%x(node foreach.js)
}
output = output + "," + time.real.to_s
time = Benchmark.measure {
%x(node forof.js)
}
output = output + "," + time.real.to_s
time = Benchmark.measure {
%x(node forloop.js)
}
output = output + "," + time.real.to_s
time = Benchmark.measure {
%x(node while.js)
}
output = output + "," + time.real.to_s
file.puts(output)
file.close
}
var arr = new Array(1000)
for(var i = 0; i < arr.length; i++){
arr[i] = i
}
var sum = 0
var i = 0
do {
sum += arr[i] * 2
i++
} while ( i < arr.length )
console.log("SUM: " + sum);
var arr = new Array(1000)
for(var i = 0; i < arr.length; i++){
arr[i] = i
}
var sum = 0
arr.forEach(i => {
sum += i * 2
})
console.log("SUM: " + sum);
var arr = new Array(1000)
for(var i = 0; i < arr.length; i++){
arr[i] = i
}
var sum = 0
for(var i = 0; i < arr.length; i++){
sum += arr[i] * 2
}
console.log("SUM: " + sum);
var arr = new Array(1000)
for(var i = 0; i < arr.length; i++){
arr[i] = i
}
var sum = 0
for(var i of arr){
sum += i * 2
}
console.log("SUM: " + sum);
var arr = new Array(1000)
for(var i = 0; i < arr.length; i++){
arr[i] = i
}
var sum = 0
var i = 0
while (i < arr.length) {
sum += arr[i] * 2
i++
}
console.log("SUM: " + sum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment