Created
April 5, 2018 03:32
-
-
Save nandobang/3ef767932cb7ece807c620c9b11f2df9 to your computer and use it in GitHub Desktop.
Simple script for benchmarking using Node and prime numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isPrime = (n) => { | |
let max = Math.floor(Math.sqrt(n)); | |
for (var i = 2; i <= max; i++) { | |
if (n % i == 0) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
const testSize = (size) => { | |
let numbers = []; | |
let startTime = (new Date()).valueOf(); | |
for (var i = 2; i < size; i++) { | |
if (isPrime(i)) { | |
numbers.push(i); | |
} | |
} | |
let endTime = (new Date()).valueOf(); | |
let timeElapsed = endTime - startTime; | |
console.log(`Took ${timeElapsed > 1000 ? timeElapsed / 1000 : timeElapsed } ${timeElapsed > 1000 ? 'seconds' : 'milliseconds' } to find ${numbers.length} prime numbers from a set of ${size} numbers.`); | |
}; | |
testSize(10); | |
testSize(100); | |
testSize(1000); | |
testSize(10000); | |
testSize(100000); | |
testSize(1000000); | |
testSize(10000000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment