Optimisation Tip: Avoid Excessive Forks+EXECVEs to Processes
This will come as a surprise to few, but performing a separate
fork()
and EXECVE
system calls (e.g: for `factor "$n"`
in Perl or popen
in C), on each iteration of a loop can really kill performance. One option
to reduce that would be to read individual lines from a
seq 2 999999 | xargs factor
loop, or avoid forking altogether.
I discovered this issue while working on Project Euler problem #141, and I should have realised that the sub-100% CPU utilisation of the program was caused by the excessive spawning of new processes.
Licence
You can reuse this entry under the Creative Commons Attribution 3.0 Unported licence, or at your option any later version. See the instructions of how to comply with it.