Enabling Multi-Processor (Parallel) Builds in Visual Studio
Building larger solutions in Visual Studio can be tediously slow. Part of the reason is that the compiler does not make use of multiple CPU cores well. This article shows you how to change that.
Enabling Parallel Builds
Enabling parallel (aka multi-processor, aka multi-core) builds is easy: just add the compiler switch /MP
(multi-processor compilation):
Dependencies
/MP
is incompatible with several other options and language features. These are discussed in Microsoft’s documentation of the /MP switch. Here is what affected us with uberAgent.
#import
If you use the #import
preprocessor directive with /MP
you will get the following compiler error:
Error C2813: #import is not supported with /MP
The easiest way to work around that is to specify /MP1
as an additional command-line option for the source file with the #import
(yes, for individual files only).
Enable Minimal Rebuild (/Gm)
This is listed as deprecated anyway, so just switch it off.
Build Time Improvements
I did not run any lengthy tests, but here is a quick comparison of a rebuild of our uberAgent project (with multiple dependent projects) with parallel builds off and on:
- Parallel builds off (default setting): 74 s
- Parallel builds on: 44 s
The simple changes to the configuration explained above reduced the build time by 41%.
More Information
Minimum Visual Studio Version
We are using Visual Studio 2019. That is what I tested with. Obviously, I cannot give any guarantees for older versions, but from what I read this should apply to any version of Visual Studio going as far back as 2008.
Resources
- Microsoft C++ Team Blog: Recommendations to speed C++ builds in Visual Studio
- Microsoft Visual C++ documentation: /MP (Build with Multiple Processes)
- Bruce Dawson: Make VC++ Compiles Fast Through Parallel Compilation
- Stack Overflow answer to the question Strategies for multicore builds (/MP) that use #import
2 Comments
It’s surprising that this option isn’t set by default for newly generated solutions, given that it provides a win at almost no cost. Almost because there are two “pitfalls”, where the second might be the reason for not setting it by default.
The first is that only cpp files using the same compiler options can be processed in parallel (as is also mentioned in Bruce Dawsons post in the references). Usually that is true for most files except for the one creating the precompiled header, if you’re using it. But I actually encountered this problem in the wild so it’s worth checking.
The second thing to consider is how to combine this with the “maximum number of parallel product builds” when your solution file contains several projects to build. Since setting /MP for each project allows every single project to occupy all CPUs, building multiple projects in parallel becomes slower, since [number of projects]*cores process are started for compilation. This might also be the reason why /MP is not active by default.
If you want to toy with the “parallel product builds” option, you can find it under the menu Tools >> Options >> Project and Solutions >> Build and Run
Hi, I’m wondering if there is a way of applying the /MP1 compiler option to one or more specific source files only within the Visual Studio 2019 gui? There seems no (accessible) documentation available (that I can see!).
Thanks!