-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
89 lines (74 loc) · 2.58 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const os = require('os');
const path = require('path');
const {Worker, isMainThread, parentPort, workerData} = require('worker_threads');
const inquirer = require('inquirer');
const ora = require('ora');
const userCPUCount = os.cpus().length;
const NS_PER_SEC = 1e9;
const workerPath = path.resolve('factorial-worker.js');
const calculateFactorialWithWorker = number => {
// Insert the array preparation code.
if (number === 0) {
return 1;
}
const numbers = [];
for (let i = 1n; i <= number; i++) {
numbers.push(i);
}
const segmentSize = Math.ceil(numbers.length / userCPUCount);
const segments = [];
console.log(numbers.length, userCPUCount, segmentSize);
for (let segmentIndex = 0; segmentIndex < userCPUCount; segmentIndex++) {
const start = segmentIndex * segmentSize;
const end = start + segmentSize;
const segment = numbers.slice(start, end);
segments.push(segment);
}
var promises = segments.map(
segment =>
new Promise((resolve, reject) => {
const worker = new Worker(workerPath, {
workerData: segment,
});
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', code => {
if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`));
});
})
)
return Promise.all(promises).then(results => {
return results.reduce((acc, val) => acc * val, 1n);
});
};
const calculatFactorial = (number) => {
const numbers = [];
for (let i = 1n; i <= number; i++) {
numbers.push(i);
}
return numbers.reduce((acc, val) => acc * val, 1n);
}
const benchmarkFactorial = async (inputNumber, factFun, label) => {
const spinner = ora(`Calculating with ${label}..`).start();
const startTime = process.hrtime();
const result = await factFun(BigInt(inputNumber));
const diffTime = process.hrtime(startTime);
const time = diffTime[0] * NS_PER_SEC + diffTime[1];
spinner.succeed(`${label} result done in: ${time}`);
return time;
}
const run = async () => {
const {inputNumber} = await inquirer.prompt([
{
type: 'input',
name: 'inputNumber',
message: 'Calculate factorial for:',
default: 10,
},
]);
const timeWorker = await benchmarkFactorial(inputNumber, calculateFactorialWithWorker, 'Worker');
const timeLocal = await benchmarkFactorial(inputNumber, calculatFactorial, 'Local');
const diff = timeLocal - timeWorker;
console.log(`Difference between local and worker: ${Math.floor(diff / 1000000)}ms`);
};
run();