When programming, is it a good idea to use multi-process all the time? Does NodeJs child_process is a good way to empower your machine power?
The answer is not awalys.
For light load, the time consumed to start a new process is much heavier than the load it self, we should not spawn multi process.
And the decreasing of time-spending is not linear with process count increasing.
The process spawning is through NodeJs Child Process.
Testing Machine and Work Load
The work load to sum 10k numbers and repeat the work from 1 to 100k times.
The spec is 6 Core Intel CPU with 16G ram.
Use Single Process for Light Work Load
As we can find from the diagram, by using single process which is only one main process, for the light work which is less than 10k * 1k plus, the single process is the best solution. Spawning multiple process can only make it more time consuming. For the work 10k * 1, the single process takes less than 1 ms, but after spawning process, it takes about 50ms, which makes performance much worse.
For Heavy Work Load, Multi Process can help a lot
When the work load becomes to 10k * 100k, as you can see in the chart, the top line. Single process takes about 500ms.
Spawning 1 child process which is 1 main process, one child process, things get worse. It takes about 620ms.
Spawning 2 and more child process, the significant benefice can be gotten, they all take less than 400ms, and the best case is in process count 5 and 6, it takes about 220ms. 6 is the number of CPU cores. The machine CPU spec is 6 cores with 12 processors, so the best case is to use all the CPU cores. With increasing the process count, the time cost increases too. When increasing to 11 and 12 which is the CPU spec process count, it takes about 280ms, when goes up to 24 which is double of CPU process count and 4 times of CPU cores count, the time cost rise into about 400.
Node child process can help to speed up your software, but only for heavy work. When you use all of your real CPU cores, you can gain the best performance of your machine, more does not make it better.