make -j4 or -j8

前端 未结 4 660
逝去的感伤
逝去的感伤 2020-12-12 20:50

I have 4 processors and am compiling processor hungry application, I read that using make with the -j4 switch was recommended for OpenCV, should I rather use -j8 and what is

4条回答
  •  隐瞒了意图╮
    2020-12-12 21:08

    As you say the -j flag tells make that it is allowed to spawn the provided amount of 'threads'. Ideally each thread is executed on its own core/CPU, so your multi-core/CPU environment is used to its fullest.

    make itself does not compile the source files. This is done by a compiler (gcc). The Makefile (input for make) contains a set of targets. Each target has a set of dependencies (on other targets) and rules how to build the target. make reads the Makefile(s) and manages all targets, dependencies and build rules. Besides compiling source files you can use make to perform any task that can be described by shell commands.

    If you set the allowed number of threads too high, it is not possible to schedule each thread on its own core. Additional scheduling (context) switches are required to let all threads execute. This additional resource usage obviously result in lower performance.

    There are multiple rules-of-thumb, but I guess that setting to total amount to + 1 is the most common. The idea behind this is that all cores have their own thread and there is one additional managing thread that handles the targets and which is next to be build.

提交回复
热议问题