Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sequencial algorithm is faster than parallel map #240

Open
ccknaus opened this issue Jul 26, 2022 · 1 comment
Open

sequencial algorithm is faster than parallel map #240

ccknaus opened this issue Jul 26, 2022 · 1 comment

Comments

@ccknaus
Copy link

ccknaus commented Jul 26, 2022

Good evening.

I tried to compare a simple sequencial vs. parallel algorithm. To my surprise, the sequiencial code seems to be faster with different maxWorker options, as well as number of ELEMENTS. My tests are not only restricted for this simple example, but for every comparision of different functions as well.

What am I doing wrong?
What is the cause of the higher latency in the parallel map?
can I increase the speed?

const ELEMENTS = 10000;
options = {
    evalPath: undefined,
    maxWorkers: 4,
    synchronous: false
};
const arr = new Array(ELEMENTS).fill().map(Math.random);

// Sequencial Algorithm
const start = performance.now();
arr.map(number => number * 7);
console.log(performance.now()-start); // about 1 ms on my machine

// Parallel Alogorithm
const start2 = performance.now();
var Parallel = require('paralleljs');
var p = new Parallel(arr, options);
log = function () { 
    console.log(performance.now()-start2); // about 360 up to 420 ms on my machine, depending on maxWorkers
};
p.map(number => number * 7).then(log); 
@Noushad-web
Copy link

@ccknaus I think the reason for the observed performance difference between the sequential and parallel versions of your code lies in the overhead associated with using parallel processing for relatively simple operations like multiplication. This overhead includes the time required to set up parallel processing, distribute tasks to worker threads, and potentially transfer data between the main thread and worker threads.

In the case of straightforward operations such as multiplying numbers, this overhead can actually be harmful, outweighing any potential performance benefits that parallelism might offer. Consequently, the sequential algorithm tends to be faster in scenarios where individual tasks are simple, like multiplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants