-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
50 lines (42 loc) · 1.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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/NS_PER_SEC} seconds\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 arrayBuffer");
const workerSharedArrayBufferTime = await calcTime(primeRange,sharedArrayBuffer,"worker sharedArrayBuffer");
const localTime = await calcTime(primeRange,normalSieve,"main");
console.log(`⏱️ Time difference between main thread and worker (sharedArrayBuffer) : ${(localTime-workerSharedArrayBufferTime)/NS_PER_SEC} seconds`);
};
run();