Answer the question
In order to leave comments, you need to log in
Why does threading take longer?
I'm trying to master the threads in the node, everything seems to work, but even when the array is divided into 4 parts and working in 4 threads, the usual code works faster. When there was a code with a factorial, everything worked fine, but when I replaced it with my own, I can’t process a large amount of information faster than single-threading. What am I doing wrong? (Original - https://github.com/BuildingXwithJS/node-worker-fac...
const os = require('os');
const path = require('path');
const CalculateArray = require('./array.js');
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 = (size, max, min, number) => {
let arr = new CalculateArray(size, max, min);
// console.log(arr.arr)
const segmentSize = Math.ceil(arr.arr.length / userCPUCount);
const segments = [];
console.log('info:', arr.arr.length, userCPUCount, segmentSize);
for (let segmentIndex = 0; segmentIndex < userCPUCount; segmentIndex++) {
const start = segmentIndex * segmentSize;
const end = start + segmentSize;
const segment = arr.arr.slice(start, end);
segments.push(segment);
}
var promises = segments.map(
segment =>
new Promise((resolve, reject) => {
const worker = new Worker(workerPath, {
workerData: {
numbers: segment,
count: number
},
});
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 100//arr.calculate(number);
});
};
const calculatFactorial = (size, max, min, number) => {
let arr = new CalculateArray(size, max, min);
return arr.calculate(number);
}
const benchmarkFactorial = async (size, max, min, inputNumber, factFun, label) => {
const spinner = ora(`Calculating with ${label}..`).start();
const startTime = process.hrtime();
const result = await factFun(size, max, min, 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, inputArrSize, inputMinNumber, inputMaxNumber} = await inquirer.prompt([
{
type: 'input',
name: 'inputArrSize',
message: 'Array size:',
default: 78060000
},
{
type: 'input',
name: 'inputMaxNumber',
message: 'Max number:',
default: 50800
},
{
type: 'input',
name: 'inputMinNumber',
message: 'Min number:',
default: 100
},
{
type: 'input',
name: 'inputNumber',
message: 'Calculate for:',
default: 15070
}
]);
const timeWorker = await benchmarkFactorial(inputArrSize, inputMaxNumber, inputMinNumber, inputNumber, calculateFactorialWithWorker, 'Worker');
const timeLocal = await benchmarkFactorial(inputArrSize, inputMaxNumber, inputMinNumber, inputNumber, calculatFactorial, 'Local');
const diff = timeLocal - timeWorker;
console.log(`Difference between local and worker: ${Math.floor(diff / 1000000)}ms`);
};
run();
const {Worker, parentPort, workerData} = require('worker_threads');
const CalculateArray = require('./array.js');
// get the array numbers
const numbers = workerData.numbers;
const count = workerData.count;
const calculate = (arr, count) => {
return CalculateArray.findCountedItemInArr(arr, count);
}
const result = calculate(numbers, count);
// return result
parentPort.postMessage(result);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question