Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Threads faster than Sequentially processing?

Just wanted to ask whether it's true that parallel processing is faster than sequentially processing. I've always thought that parallel processing is faster, so therefore, I did an experiment. I benchmarked my scripts and found out that after doing a bunch of

sub add{

    for ($x=0; $x<=200000; $x++){
        $data[$x] = $x/($x+2);
    }

}

threading seems to be slower by about 0.5 CPU secs on average. Is this normal or is it really true that sequentially processing is faster?

like image 947
robobooga Avatar asked Aug 18 '26 06:08

robobooga


2 Answers

Whether parallel vs. sequential processing is better is highly task-dependent and you've already done the right thing: You benchmarked both and determined for your task (the one you benchmarked, not necessarily the one you actually want to do) which one is faster.

As a general rule, on a single processor, sequential processing tends to be better for tasks which are CPU-bound, because if you have two tasks each needing five seconds of CPU time to complete, then you'll need ten seconds of CPU time regardless of whether you do them sequentially or in parallel. Setting up multiple threads/processes will, therefore, provide no benefit, but it will create additional task-switching overhead while also preventing you from having any results until all results are available.

CPU-bound tasks on a multi-processor system tend to do better when run in parallel, provided that they can run independently of each other. If not, or if you're using a language/threading model/IPC model/etc. which forces all tasks to run on the same processor, then see "on a single processor" above.

Parallel processing is generally better for tasks which are I/O-bound, regardless of the number of processors available, because CPUs are fast and I/O is slow, so working in parallel allows one task to process its data while the other is waiting for I/O operations to complete. (This is why make -j2 tends to be significantly faster than a plain make, even on single-processor machines.)

But, again, these are all generalities and all have cases where they'll be incorrect. Only benchmarking will reveal the truth with certainty.

like image 187
Dave Sherohman Avatar answered Aug 19 '26 20:08

Dave Sherohman


Perl threads are an extreme suck. You are better off in every case forking several processes.

When you create a new thread in perl, it does the following:

  • Make a copy - yes, a real copy - of every single perl data structure in scope, including those belonging to modules you didn't write
  • Start up what is almost a new, independent instance of perl in a new OS thread

If you then want to share anything (as it has now copied everything) you have to use the share function in the threads module. This is incredibly sucky, as it replaces your variable, with some tie() nonsense, which adds much-too-fine-grained locking around it to prevent concurrent access. Accessing a shared variable then causes a massive amount of implicit locking, and is incredibly slow.

So in short, perl threads:

  • Take a long time to start
  • waste loads of memory
  • Cannot share data efficiently anyway.

You are much better off with fork(), which does not copy every variable (the kernel does copy-on-write) unless you're on Windows.

like image 22
MarkR Avatar answered Aug 19 '26 19:08

MarkR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!