Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nilshah98
committed
Mar 29, 2019
1 parent
27cb8c7
commit 7ae525e
Showing
11 changed files
with
140 additions
and
253 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
- Difference between `var func = () => {}` v/s `var func = () => ()` v/s `var func = () => `. | ||
- After a `Promise` is resolved, it cannot go back to pending state. More so, causing `Promise.all()` to execute for each single instance as well, now. | ||
- Need to fill Array object before passing, else recieve something like - `<2 empty items>` in console. | ||
- Need to fill Array object before passing, else recieve something like - `<2 empty items>` in console. | ||
- Fixed relative path issues while exporting, use `__dirname` or other special variables like `__filename` |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const Benchmark = require('benchmark'); | ||
const suite = new Benchmark.Suite; | ||
const sharedBufferPrime = require("./sharedArrayBuffer/main") | ||
|
||
|
||
|
||
const inps = [100000000]; | ||
inps.forEach((num) => { | ||
|
||
suite.add(String(num) + " test", { | ||
defer: true, | ||
fn: async (deferred) => { | ||
await sharedBufferPrime.calcPrime(num); | ||
deferred.resolve(); | ||
} | ||
}) | ||
}) | ||
|
||
suite | ||
// add listeners | ||
.on('cycle', function(event) { | ||
console.log(event.target.name); | ||
console.log(event.target.stats.mean); | ||
console.log(event.target.stats.sample.length); | ||
}) | ||
.on('complete', function() { | ||
// console.log('Fastest is ' + this.filter('fastest').map('name')); | ||
}) | ||
// run async | ||
.run({ 'async': true }); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const inquirer = require("inquirer"); | ||
const ora = require("ora"); | ||
const array = require("./array"); | ||
const arrayBuffer = require("./arrayBuffer"); | ||
const sharedArrayBuffer = require("./sharedArrayBuffer"); | ||
const normalSieve = require("./normalSieve"); | ||
|
||
const NS_PER_SEC = 1e9; | ||
|
||
const calcTime = async (inp,func,label) => { | ||
const spinner = ora("Calculating number of primes-").start(); | ||
const stTime = process.hrtime(); | ||
var res = await func(inp); | ||
const endTime = process.hrtime(stTime); | ||
spinner.succeed(`(${label}) Number of primes : ${res}`); | ||
const time = endTime[0] * NS_PER_SEC + endTime[1]; | ||
spinner.stopAndPersist( | ||
{ | ||
"text":`(${label}) Benchmark took : ${time} nanoseconds\n`, | ||
"symbol":"⌛" | ||
} | ||
); | ||
return time; | ||
} | ||
|
||
const run = async () => { | ||
const { primeRange } = await inquirer.prompt([ | ||
{ | ||
type: "input", | ||
name: "primeRange", | ||
message: "Find primes till ?", | ||
default: 100 | ||
} | ||
]); | ||
|
||
// Written using then | ||
// calculatePrimes(primeRange).then((res) => { | ||
// spinner.succeed(`Number of primes : ${res[0]}`) | ||
// }); | ||
|
||
const workerArrayTime = await calcTime(primeRange,array,"worker array"); | ||
const workerArrayBufferTime = await calcTime(primeRange,arrayBuffer,"worker arrayBufer"); | ||
const workerSharedArrayBufferTime = await calcTime(primeRange,sharedArrayBuffer,"worker sharedArrayBuffer"); | ||
const localTime = await calcTime(primeRange,normalSieve,"main"); | ||
// console.log(`⏱️ Time difference between worker and main thread : ${localTime-workerTime} nanoseconds`); | ||
|
||
}; | ||
|
||
run(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const normalSieve = n => { | ||
return new Promise((resolve, reject) => { | ||
// Eratosthenes algorithm to find all primes under n | ||
var array = [], | ||
upperLimit = Math.sqrt(n), | ||
output = []; | ||
|
||
// Make an array from 2 to (n - 1) | ||
for (var i = 0; i < n; i++) { | ||
array.push(true); | ||
} | ||
|
||
// Remove multiples of primes starting from 2, 3, 5,... | ||
for (var i = 2; i <= upperLimit; i++) { | ||
if (array[i]) { | ||
for (var j = i * i; j < n; j += i) { | ||
array[j] = false; | ||
} | ||
} | ||
} | ||
|
||
// All array[i] set to true are primes | ||
for (var i = 2; i < n; i++) { | ||
if (array[i]) { | ||
output.push(i); | ||
} | ||
} | ||
|
||
resolve(output.length); | ||
}); | ||
}; | ||
|
||
module.exports = normalSieve; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.